Back

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

Could someone tell me how to remove the beginning and trailing spaces in JAVA output?

1 Answer

0 votes
by (11.3k points)

If 'str' is a String variable with the possibility of it containing white spaces that you need removed, then use the trim() function to attain the desired output/string value:

System.out.println(str.trim());

OR 

str_out=str.trim(); 

System.out.println(str_out);

//if str="     Hello, have a nice day    " 

//Output:

//Hello, have  a nice day

If you're getting into JAVA, be sure to check out this Java course for a smooth learning experience:

Browse Categories

...