Java String trim example

String’s trim method is used to remove leading and trailing space. Sometimes, you get extra leading or trailing space and your program does not work as expected. It returns String object after removing leading and trailing spaces.

String’s trim method does not remove middle spaces.

String trim example:

When you run above program, you will get below output:

Add Comment