What's the difference between replaceAll("\\s+")
and replaceAll("\\\\s+")
? Usually I use \\s+
but sometimes I see \\\\s+
.
Java replaceAll("\\s+") vs replaceAll("\\\\s+") [duplicate]
Asked Answered
\ is written as \\ in Java. This should help you. –
Barroom
This should help to solve your problem: #4654331 –
Fye
\\s+
--> replaces 1 or more spaces.
\\\\s+
--> replaces the literal \
followed by s one or more times.
Code:
public static void main(String[] args) {
String s = "\\sbas def";
System.out.println(s);
System.out.println(s.replaceAll("\\s+", ""));
System.out.println(s.replaceAll("\\\\s+", ""));
}
O/P :
\sbas def
\sbasdef
bas def
"...followed by s one or more times." Do u mean symbol "s" or spaces? –
Filagree
But where is the first "\" here "\sbasdef"? –
Filagree
@MaximGotovchits - the character
s
not spaces. The first `\` will be escaped. –
Capstone It also removes control characters like new line, tab, etc. Example: " \r \n\n \t ".replaceAll("\\s+","" ).length() == 0 >> true –
Cotidal
© 2022 - 2024 — McMap. All rights reserved.