Intellipaat Back

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

I would like to remove all the whitespaces between the letters. Can anyone help me with this?

1 Answer

0 votes
by (13.1k points)

You can tell replace() to repeat the regex:

.replace(/ /g,’ ’)

G character makes it a “global” match which means it would repeat through the entire string.

If you want to match all whitespace and not just the space character use \s instead:

.replace(/\s/g,’ ‘)

You can also use .replaceAll() like this:

.replaceAll(/\s/g,’ ‘)

Want to learn Java? Check out the Java certification from Intellipaat.

Related questions

0 votes
1 answer
asked Jan 18, 2021 in BI by Chris (11.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...