String replace() returns extra space in Java
Clash Royale CLAN TAG#URR8PPP
up vote
16
down vote
favorite
Consider:
System.out.println(new String(new char[10]).replace("", "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?
java string replace char
add a comment |Â
up vote
16
down vote
favorite
Consider:
System.out.println(new String(new char[10]).replace("", "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?
java string replace char
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
1
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43
add a comment |Â
up vote
16
down vote
favorite
up vote
16
down vote
favorite
Consider:
System.out.println(new String(new char[10]).replace("", "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?
java string replace char
Consider:
System.out.println(new String(new char[10]).replace("", "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?
java string replace char
edited Aug 20 at 10:15
Sun
13.1k31541
13.1k31541
asked Aug 18 at 16:02
Ansh
964
964
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
1
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43
add a comment |Â
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
1
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
1
1
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
14
down vote
It is not a space. It is how your IDE/console shows character which
new char[10]
is filled with by default.
You are not replacing 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 hello
s (not 10), without modifying , 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
""
withX
will result in"XaXaXaX"
(but in your case instead ofa
your console will printusing character which will look like space)
add a comment |Â
up vote
11
down vote
Short version
represents character
NUL
, it does not equals empty string ""
.
Long version
When you try to create a
String
with emptychar[10]
,:String input = new String(new char[10]);
this
String
will contains 10NUL
character:|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
When you call
input.replace("", "hello")
, theNUL
value() will be replaced by
hello
:|hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
When you call
input.replace("", "hello")
, theNUL
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|
add a comment |Â
up vote
7
down vote
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
using the method thus results in:
hellohellohellohellohellohellohellohellohellohello
Display
Your console probably displayed the character as whitespace, while it's actually not a whitespace but
.
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. ).
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.
â Radiodef
Aug 19 at 2:45
add a comment |Â
up vote
3
down vote
char
's default value is u0000
which can also be represented as . So your
new char[10]
contains 10 s.
In the first statement you clearly replace with
"hello"
. But in the second statement you leave out the default value. Which your IDE output chooses to show as a space.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
It is not a space. It is how your IDE/console shows character which
new char[10]
is filled with by default.
You are not replacing 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 hello
s (not 10), without modifying , 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
""
withX
will result in"XaXaXaX"
(but in your case instead ofa
your console will printusing character which will look like space)
add a comment |Â
up vote
14
down vote
It is not a space. It is how your IDE/console shows character which
new char[10]
is filled with by default.
You are not replacing 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 hello
s (not 10), without modifying , 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
""
withX
will result in"XaXaXaX"
(but in your case instead ofa
your console will printusing character which will look like space)
add a comment |Â
up vote
14
down vote
up vote
14
down vote
It is not a space. It is how your IDE/console shows character which
new char[10]
is filled with by default.
You are not replacing 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 hello
s (not 10), without modifying , 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
""
withX
will result in"XaXaXaX"
(but in your case instead ofa
your console will printusing character which will look like space)
It is not a space. It is how your IDE/console shows character which
new char[10]
is filled with by default.
You are not replacing 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 hello
s (not 10), without modifying , 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
""
withX
will result in"XaXaXaX"
(but in your case instead ofa
your console will printusing character which will look like space)
edited Aug 18 at 17:17
answered Aug 18 at 16:16
Pshemo
91.1k14127178
91.1k14127178
add a comment |Â
add a comment |Â
up vote
11
down vote
Short version
represents character
NUL
, it does not equals empty string ""
.
Long version
When you try to create a
String
with emptychar[10]
,:String input = new String(new char[10]);
this
String
will contains 10NUL
character:|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
When you call
input.replace("", "hello")
, theNUL
value() will be replaced by
hello
:|hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
When you call
input.replace("", "hello")
, theNUL
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|
add a comment |Â
up vote
11
down vote
Short version
represents character
NUL
, it does not equals empty string ""
.
Long version
When you try to create a
String
with emptychar[10]
,:String input = new String(new char[10]);
this
String
will contains 10NUL
character:|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
When you call
input.replace("", "hello")
, theNUL
value() will be replaced by
hello
:|hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
When you call
input.replace("", "hello")
, theNUL
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|
add a comment |Â
up vote
11
down vote
up vote
11
down vote
Short version
represents character
NUL
, it does not equals empty string ""
.
Long version
When you try to create a
String
with emptychar[10]
,:String input = new String(new char[10]);
this
String
will contains 10NUL
character:|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
When you call
input.replace("", "hello")
, theNUL
value() will be replaced by
hello
:|hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
When you call
input.replace("", "hello")
, theNUL
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|
Short version
represents character
NUL
, it does not equals empty string ""
.
Long version
When you try to create a
String
with emptychar[10]
,:String input = new String(new char[10]);
this
String
will contains 10NUL
character:|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|NUL|
When you call
input.replace("", "hello")
, theNUL
value() will be replaced by
hello
:|hello|hello|hello|hello|hello|hello|hello|hello|hello|hello|
When you call
input.replace("", "hello")
, theNUL
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|
edited Aug 18 at 19:15
answered Aug 18 at 16:19
Sun
13.1k31541
13.1k31541
add a comment |Â
add a comment |Â
up vote
7
down vote
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
using the method thus results in:
hellohellohellohellohellohellohellohellohellohello
Display
Your console probably displayed the character as whitespace, while it's actually not a whitespace but
.
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. ).
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.
â Radiodef
Aug 19 at 2:45
add a comment |Â
up vote
7
down vote
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
using the method thus results in:
hellohellohellohellohellohellohellohellohellohello
Display
Your console probably displayed the character as whitespace, while it's actually not a whitespace but
.
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. ).
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.
â Radiodef
Aug 19 at 2:45
add a comment |Â
up vote
7
down vote
up vote
7
down vote
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
using the method thus results in:
hellohellohellohellohellohellohellohellohellohello
Display
Your console probably displayed the character as whitespace, while it's actually not a whitespace but
.
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. ).
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
using the method thus results in:
hellohellohellohellohellohellohellohellohellohello
Display
Your console probably displayed the character as whitespace, while it's actually not a whitespace but
.
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. ).
edited Aug 18 at 16:21
answered Aug 18 at 16:15
Zabuza
10.6k52538
10.6k52538
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.
â Radiodef
Aug 19 at 2:45
add a comment |Â
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.
â Radiodef
Aug 19 at 2:45
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.â Radiodef
Aug 19 at 2:45
replace("", ...)
inserts the replacement at the end of the string too. See e.g. ideone.com/492Cml.â Radiodef
Aug 19 at 2:45
add a comment |Â
up vote
3
down vote
char
's default value is u0000
which can also be represented as . So your
new char[10]
contains 10 s.
In the first statement you clearly replace with
"hello"
. But in the second statement you leave out the default value. Which your IDE output chooses to show as a space.
add a comment |Â
up vote
3
down vote
char
's default value is u0000
which can also be represented as . So your
new char[10]
contains 10 s.
In the first statement you clearly replace with
"hello"
. But in the second statement you leave out the default value. Which your IDE output chooses to show as a space.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
char
's default value is u0000
which can also be represented as . So your
new char[10]
contains 10 s.
In the first statement you clearly replace with
"hello"
. But in the second statement you leave out the default value. Which your IDE output chooses to show as a space.
char
's default value is u0000
which can also be represented as . So your
new char[10]
contains 10 s.
In the first statement you clearly replace with
"hello"
. But in the second statement you leave out the default value. Which your IDE output chooses to show as a space.
answered Aug 18 at 16:28
Roshana Pitigala
3,57162342
3,57162342
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51910381%2fstring-replace-returns-extra-space-in-java%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You should loop through the string and print each character.
â copper.hat
Aug 18 at 22:56
1
@Ferrybig There was never a trailing space. The OP's original description was (presumably) incorrect. See for example ideone.com/492Cml.
â Radiodef
Aug 19 at 2:43