String s="Swamy Application";
s=s.replaceAll("\\S"," ");
system.out.println(s);
Should return String but we are getting empty
I need explanation What happening in \\S
.
String s="Swamy Application";
s=s.replaceAll("\\S"," ");
system.out.println(s);
Should return String but we are getting empty
I need explanation What happening in \\S
.
String.replaceAll()
takes a regex as first parameter and the replacement text for all matches of that regex as second parameter. Here, you have given \\S
as the first parameter, which matches every non-whitespace character. The replacement string given is a whitespace. So the returned String would be having whitespaces only.
\S matches any non-whitespace character which is leading to replace the alpha characters in the string to whitespace.
"Swamy Application" -> " "
More about this at source
If you are trying to replace the whitespace character from the string then use:
"\s"
else if you are trying to replace the only S character from the string then use:
"S"