trim():Returns a copy of the string, with leading and trailing white space omitted.
Syntax:
public String trim()
Example:
StringTrimExample.java
/** * This program is used to show the use of trim() method. * @author w3spoint */ class TestString{ String str = " www.w3spoint.com "; /** * This method is used to show the use of trim() method. * @author w3spoint */ public void trimString(){ //will remove all leading and trailing whitespace. System.out.println(str.trim()); } } public class StringTrimExample { public static void main(String args[]){ //creating TestString object TestString obj = new TestString(); //method call obj.trimString(); } } |
Output:Â
www.w3spoint.com |
Download this example.
Next Topic: StringBuffer in java.
Previous Topic: length() String functions in java with example.