Empty Strings within a non empty String [duplicate]

Empty Strings within a non empty String [duplicate]






This question already has answers here:
                            
                        



Replace with empty string replaces newChar around all the characters in original string

                                    (4 answers)
                                

Closed 3 years ago.


I'm confused with a code
public class StringReplaceWithEmptyString 
{
    public static void main(String[] args) 
    {
        String s1 = "asdfgh";
        System.out.println(s1);
        s1 = s1.replace("", "1");
        System.out.println(s1); 
    }
}

And the output is:
asdfgh
1a1s1d1f1g1h1

So my first opinion was every character in a String is having an empty String "" at both sides. But if that's the case after 'a' (in the String) there should be two '1' coming in the second line of output (one for end of 'a' and second for starting of 's').
Now I checked whether the String is represented as a char[] in these links In Java, is a String an array of chars? and String representation in Java I got answer as YES.
So I tried to assign an empty character '' to a char variable, but its giving me a compiler error,

Invalid character constant 

The same process gives a compiler error when I tried in char[] 
char[] c = {'','a','','s'};  // CTE

So I'm confused about three things. 

How an empty String is represented by char[] ?
Why I'm getting that output for the above code?
How the String s1 is represented in char[] when it is initialized first time?

Sorry if I'm wrong at any part of my question.

Solutions/Answers:

Answer 1:

Going with Andy Turner’s great comment, your call to String#replace() is actually implemented using String#replaceAll(). As such, there is a regex replacement happening here. The matches occurs before the first character, in between each character in the string, and after the last character.

^|a|s|d|f|g|h|$
 ^ this and every pipe matches to empty string ""

The match you are making is a zero length match. In Java’s regex implementation used in String.replaceAll(), this behaves as the example above shows, namely matching each inter-character position and the positions before the first and after the last characters.

Here is a reference which discusses zero length matches in more detail: http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/

A zero-width or zero-length match is a regular expression match that does not match any characters. It matches only a position in the string. E.g. the regex \b matches between the 1 and , in 1,2.

Answer 2:

Just adding some more explanation to Tim Biegeleisen answer.

As of Java 8, The code of replace method in java.lang.String class is

public String replace(CharSequence target, CharSequence replacement) {
        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}

Here You can clearly see that the string is replaced by Regex Pattern matcher and in regex “” is identified by Zero-Length character and it is present around any Non-Zero length character.

So, behind the scene your code is executed as following

Pattern.compile("".toString(), Pattern.LITERAL).matcher("asdfgh").replaceAll(Matcher.quoteReplacement("1".toString()));

The the output becomes

1a1s1d1f1g1h1

Answer 3:

This is because it does a regex match of the pattern/replacement you pass to the replace().

 public String replace(CharSequence target, CharSequence replacement) {
    return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
     this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
 }

Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence. The
replacement proceeds from the beginning of the string to the end, for
example, replacing “aa” with “b” in the string “aaa” will result in
“ba” rather than “ab”.

Parameters:

target The sequence of char values
to be replaced

replacement The replacement sequence of char values

Returns: The resulting string

Throws: NullPointerException if target
or replacement is null.

Since:
1.5

Please read more at the link below … (Also browse through the source code).

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.replace%28java.lang.CharSequence%2Cjava.lang.CharSequence%29

A regex such as “” would match every possible empty string in a string. In this case it happens to be every empty space at the start and end and after every character in the string.

References