String trim method is used to remove the leading and trailing white spaces in a String.
1 |
public String trim() |
String trim method example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kscodes.sampleproject; public class StringTrimExample { public static void main(String[] args) { String testStr1 = " Make me Slim trim "; System.out.println("Original String ::" + testStr1); System.out.println("Original String length ::" + testStr1.length()); System.out.println("----------------------------------"); System.out.println("After trimming ::"+ testStr1.trim()); System.out.println("After trimming String length ::"+ testStr1.trim().length()); } } |
Output
1 2 3 4 5 |
Original String :: Make me Slim trim Original String length ::25 ---------------------------------- After trimming ::Make me Slim trim After trimming String length ::17 |