String method trim() in java
The String method trim() used to remove leading and trailing white spaces from string. It returns string by removing leading and trailing white spaces from the string. If doesn't remove white spaces after first character and before last character if any.
Syntax
 1 public String trim()
return : It returns string after removing leading and trailing white spaces from source string.
Example
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         // string with leading and trailing white spaces
 8         String str = "   Hello world   ";
 9 
 10         String res = str.trim();
 11         System.out.println("Resulting string : " + res);
 12     }
 13 }
In the above example, a string is define by assigning a string literal that contain leading and trailing white spaces. A trim() method called that returns string after removing leading and trailing white spaces. A resulting string is assigned to the variable that will be printed.
Output
 1 Resulting string : Hello world

String with white spaces between words

String with white spaces between words
 1 package com.java;
 2 
 3 public class Example {
 4 
 5     public static void main(String[] args) {
 6 
 7         // string with white spaces between words
 8         String str = "Hello     world";
 9 
 10         String res = str.trim();
 11         System.out.println("Resulting string : " + res);
 12     }
 13 }
In the above example, a string is define that contain multiple white spaces between the words. A trim() method called that returns a same string as it does not remove white spaces between the worlds.
Output
 1 Resulting string : Hello     world

trim() with Stream API

trim() with Stream API
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList(" hello ", " world", "in ", "Java");
 11 
 12         List<String> res = list.stream()
 13                 .map(String::trim).toList();
 14 
 15         System.out.println("Resulting list : " + res);
 16     }
 17 }
In the above example, a List of string are created using Arrays static method asList(). An elements are stream using stream() method. A map() method map elements of list by trimming white spaces from each elements and return stream object that includes trimmed elements. An elements are converted to list and assigned to the variable.
Output
 1 Resulting list : [hello, world, in, Java]

String method trim() with collection

String method trim() with collection
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 public class Example {
 7 
 8     public static void main(String[] args) {
 9 
 10         List<String> list = Arrays.asList("  Starting", "with  ", "  string  ");
 11 
 12         list.forEach(ele -> {
 13             System.out.println(ele.trim());
 14         });
 15     }
 16 }
In the above example, a list of string define and iterated using collection method forEach() by specifying a lambda expression. It calls string method trim() that removes leading and trailing white spaces and print resulting string.
Output
 1 Starting
 2 with
 3 string
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us