Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replace() returns extra space in Java

Consider:

System.out.println(new String(new char[10]).replace("\0", "hello"));

has output:

hellohellohellohellohellohellohellohellohellohello 

but:

System.out.println(new String(new char[10]).replace("", "hello")); 

has output:

hello hello hello hello hello hello hello hello hello hello

Where are these extra spaces coming from?

like image 517
Ansh Avatar asked Aug 18 '18 16:08

Ansh


People also ask

How do I remove extra spaces from a string in Java?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

How do you get rid of extra spaces in a string?

Use JavaScript's string. replace() method with a regular expression to remove extra spaces. The dedicated RegEx to match any whitespace character is \s . Expand the whitespace selection from a single space to multiple using the \s+ RegEx.

How do I fix whitespace in Java?

replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n ).

How do you remove new line and space from a string in Java?

Use String. trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.


3 Answers

It is not a space. It is how your IDE/console shows \0 character which new char[10] is filled with by default.

You are not replacing \0 with anything, so it stays in string. Instead with .replace("", "hello") you are replacing only empty string "". Important thing is that Java assumes that "" exists at:

  • start of the string,
  • end of string,
  • and between each of the characters

since we can get "abc" with:

"abc" = "" + "a" + "" + "b" + "" + "c" + ""`;
      //^          ^          ^          ^

Now .replace("", "hello") replaces each of those "" with "hello", so for string of length 10 it will place additional 11 hellos (not 10), without modifying \0, which will be shown at your output like spaces.


Maybe this will be easier to grasp:

System.out.println("aaa".replace("", "X"));
  • lets represent each "" with as |. We will get "|a|a|a|" (notice that there are 4 |)
  • so replacing "" with X will result in "XaXaXaX" (but in your case instead of a your console will print \0 using character which will look like space)
like image 78
Pshemo Avatar answered Oct 14 '22 15:10

Pshemo


Short version

\0 represents character NUL, it does not equals empty string "".

Long version

  1. When you try to create a String with empty char[10],:

    String input = new String(new char[10]);
    

    this String will contains 10 NUL character:

    |NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
    
  2. When you call input.replace("\0", "hello"), the NUL value(\0) will be replaced by hello:

    |hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
    
  3. When you call input.replace("", "hello"), the NUL value will not be replaced since it does not match empty string "":

    |hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|NUL|hello|
    
like image 26
xingbin Avatar answered Oct 14 '22 16:10

xingbin


Explanation

You are using the method String#replace(CharSequence target, CharSequence replacement) (documentation).

If invoked with an empty target character sequence replace("", replacement) it will not replace the elements in the source, but insert the replacement before every character.

This is because "" matches the positions between the characters, not the characters themselves. So every position between will be replaced, i.e. replacement is inserted.

Example:

"abc".replace("", "d") // Results in "dadbdcd"

Your string contains only the default value of char at every position, it is

\0\0\0\0\0\0\0\0\0\0

using the method thus results in:

hello\0hello\0hello\0hello\0hello\0hello\0hello\0hello\0hello\0hello\0

Display

Your console probably displayed the character \0 as whitespace, while it's actually not a whitespace but \0.

If I try out your code in a different console, I get:

enter image description here

Confirming that the characters are indeed not spaces but something different (i.e. \0).

like image 25
Zabuzard Avatar answered Oct 14 '22 17:10

Zabuzard