The java lang.string.trim()is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is ‘\u0020’. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

Syntax:
public String trim() The method accepts no parameters.
Returns:
It returns the omitted string with no leading and trailing spaces.
Example: To show the working of string trim() function.
// Java program to demonstrate working // of java string trim() method class Gfg { // driver code public static void main(String args[]) { // trims the trailing and leading spaces String s = " geeks for geeks has all java functions to read "; System.out.println(s.trim()); // trims the leading spaces s = " Chetna loves reading books"; System.out.println(s.trim()); } } |
Output:
geeks for geeks has all java functions to read Chetna loves reading books
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Trim (Remove leading and trailing spaces) a string in Java
- Collator compare(String, String) method in Java with Example
- Collator equals(String, String) method in Java with Example
- Java String isEmpty() method with example
- Java String contains() method with example
- Java String charAt() method with example
- CollationElementIterator setText(String) method in Java with Example
- Java String toCharArray() with example
- Java 8 | DoubleToIntFunction Interface in Java with Example
- Collections.singleton() method in Java with example
- Java Math asin() method with Example
- Java Math log() method with example
- Java Math hypot() method with Example
- Java Math exp() method with Example
- Java Math getExponent() method with Example
- Java Math getExponent() method with Example
- Java Math expm1() method with Example
- Java Math round() method with Example
- Math pow() method in Java with Example
- Java toRadians() method with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

