Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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 (46k 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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...