Empty Strings within a non empty String

Arun Sudhakaran :

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.

  1. How an empty String is represented by char[] ?
  2. Why I'm getting that output for the above code?
  3. 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.

Tim Biegeleisen :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=451764&siteId=1