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?
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.
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.
replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n ).
Use String. trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.
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:
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 hello
s (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"));
""
with as |
. We will get "|a|a|a|"
(notice that there are 4 |
)""
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)\0
represents character NUL
, it does not equals empty string ""
.
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|
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|
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|
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
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:
Confirming that the characters are indeed not spaces but something different (i.e. \0
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With