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 (46.1k 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

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 10, 2019 in Java by tara92 (920 points)
+3 votes
3 answers
asked May 28, 2019 in Python by Anvi (10.2k points)
0 votes
0 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...