Java String trim() Method (original) (raw)
Last Updated : 27 Nov, 2024
The**trim()
**method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is “\u0020”. It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves the original string unchanged.
**Example:
Below is an example that demonstrates the usage of the **trim() method in Java.
Java `
// Java program to demonstrate the // usage of the trim() method public class Trim {
public static void main(String[] args) {
String s1 = " Hello, Geeks ";
// Using trim() to remove
// leading and trailing whitespaces
String s2 = s1.trim();
System.out.println("Original String: '" + s1 + "'");
System.out.println("Trimmed String: '" + s2 + "'");
}
}
`
Output
Original String: ' Hello, Geeks ' Trimmed String: 'Hello, Geeks'
Table of Content
**Syntax of trim() Method
String trimmedString = str.trim();
- **Parameter: The trim() method accepts no parameters.
- **Return Type: It returns a new string with leading and trailing whitespace removed. If no whitespace exists, it returns the original string unchanged.
**Examples of trim() in Java
1. Using trim() to Remove Leading and Trailing Whitespaces
In this example, we will demonstrate how the trim()
method works by removing whitespaces from the beginning and end of the string.
Java `
// Java program to demonstrate working // of java string trim() method import java.io.*;
class GFG {
public static void main (String[] args) {
// Three strings declared
String x = "geeks ";
String y = "for ";
String z = " geeks";
// Printing without trim function
System.out.println(x + y + z);
// Using trim function to get result
System.out.println(x.trim() + y.trim() + z.trim());
}
}
`
Output
geeks for geeks geeksforgeeks
**Explanation: In this example, the trim() method is used to remove the starting and ending spaces from each string before concatenating them.
2. Use trim() that Does Not Modify the Original String
The trim() method returns the string rather than making changes to the original string.
**Example:
Java `
// Java program to demonstrate working // of java string trim() method public class Trim {
public static void main(String args[]) {
// String with leading and trailing spaces
String s = " Geeks for Geeks ";
// Printing String after removing the whitespaces
System.out.println(s.trim());
// Printing string to observe original string
System.out.println(s);
}
}
`
Output
Geeks for Geeks Geeks for Geeks
**Explanation: As we can see, the trim() method
returns a new string with the whitespaces removed, but the original string “s"
remains unchanged.
3. Use trim() to Compare the Original and Trimmed Strings
When we use the trim() method, we get the original and trimmed string. Both are different, in case we are removing the whitespaces from the original string.
**Example:
Java `
// Java program to demonstrate working // of java string trim() method public class Trim {
public static void main(String[] args) {
// String with leading and trailing spaces
String s1 = " Geeks For Geeks ";
// Before Trim() method
System.out.println("String 1: " + s1);
System.out.println("Length: " + s1.length());
// Applying trim() method on string s1
String s2 = s1.trim();
// After Trim() method
System.out.println("\nString 2: " + s2);
System.out.println("Length: " + s2.length());
// Comparing both the strings
if (s1 == s2) {
System.out.println("\nEqual");
} else {
System.out.println("\nNot Equal");
}
}
}
`
Output
String 1: Geeks For Geeks
Length: 21
String 2: Geeks For Geeks Length: 15
Not Equal
**Explanation: In this example, the trim()
method creates a new string without modifying the original string. As a result, comparing the original string s1
and the trimmed string s2
shows that they are not the same.