Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty Strings within a non empty String [duplicate]

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.

like image 765
Arun Sudhakaran Avatar asked Jan 27 '17 07:01

Arun Sudhakaran


2 Answers

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
like image 163
Avinash Avatar answered Oct 13 '22 08:10

Avinash


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.

like image 36
Tim Biegeleisen Avatar answered Oct 13 '22 06:10

Tim Biegeleisen