0 votes
1 view
in Java by (3.4k points)

I have a string like this:

My = "name=avi age=12 year=2000";

I want to remove the whitespaces in the string. I tried to 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:

my = "name=aman=12year=2000"

1 Answer

0 votes
by (26.5k points)

replaceAll() method:

To remove all white spaces from String, use replaceAll() method of String class with two arguments, i.e.

 replaceAll("\\t", "");

where \\t is a single space in unicode

Syntax:

class BlankSpace { 

    public static void main(String[] args) 

    { 

        String str = "      x y z ";

        // Call the replaceAll() method 

        str = str.replaceAll("\\t", "");       

        System.out.println(str); 

    } 

Output:

xyz

...