In this example, you will see how to remove the leading and trailing whitespace of string.
Remove the leading and trailing whitespace of string.
In this example, you will see how to remove the leading and trailing whitespace of string. The trim() is a method of String class. It returns a copy of given string, after removing leading and trailing whitespace.
Code:TrimString.java
public class TrimString {
public static void main(String[] args) {
// Create a string.
String str = " The trim is a method of String class. ";
System.out.println("String : \n" + str);
// Remove leading and trailing whitespace.
String strTrim = str.trim();
System.out
.println("String after removing leading and trailing whitespace : ");
System.out.println(strTrim);
}
}
Output:
|
String : The trim is a method of String class. String after removing leading and trailing whitespace : The trim is a method of String class. |

[ 0 ] Comments