-2
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.

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42

2 Answers2

0

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.

Naveed S
  • 5,106
  • 4
  • 34
  • 52
0

\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"

Amar Myana
  • 33
  • 4