Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (13.1k points)

Can anyone help me understand the difference between these expressions:

x = x.replaceAll("\\s", "");

x = x.replaceAll("\\s+", "");

2 Answers

0 votes
by (26.7k points)

Basically, \\s helps us to match for the single whitespace character whereas, \\s+ helps us to match the sequence of more than one whitespace character. It would be more efficient if you use \\s+.

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Training now!!

0 votes
by (1.8k points)

Regular expressions(regex) in java are used for matching patterns in Strings.

The expressions \\s & \\ s+ both are used to match white spaces characters but they behave differently. \\s: This matches any single whitespace character which will include newline,unicode white space characters,spaces & tabs. \\s+ : This Matches one or more white space characters and the + quantifier means one or more of the preceding element.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Nov 13, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...