Intellipaat Back

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

Is there a convenience method to strip any leading or trailing spaces from a Java String?

Something like:

String myString = "  keep this  ";

String stripppedString = myString.strip();

System.out.println("no spaces:" + strippedString);

Result:

no spaces:keep this

myString.replace(" ","") would replace the space between keep and this.

Thanks

1 Answer

0 votes
by (46k points)

You can apply the trim() way.

String newString = oldString.trim();

Check Out javadocs

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 22, 2019 in Java by Shubham (3.9k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...