Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (920 points)

I have a string like this:

mysz = "name=john age=13 year=2001";

I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I also tried replaceAll("\\W", "") but then the = also gets removed.

How can I achieve a string with:

mysz2 = "name=johnage=13year=2001"

1 Answer

0 votes
by (13.2k points)

To remove all the whitespaces in between a string in java you can use java. replaceAll() method.It’s syntax is as shown below

String.replaceAll(String regex, String replacement) 

Regex is the string to be replaced and replacement is the string to be replaced with.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers


108k users

Browse Categories