Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The trim() method in Java is a vital String function that removes leading and trailing whitespace from a String. In this article, we will explore the purpose and usage of trim() through examples.
In Java, whitespace characters include spaces, tabs, newlines, carriage returns, and form feeds. These are represented by the Unicode value ‘\\u0020’. The trim() method helps remove extra whitespace from the beginning or end of a string. However, it does not affect any spaces in the middle of the string.
Purpose of Java trim() Method
The main purpose of Java trim() is to:
- Remove leading whitespace characters
- Remove trailing whitespace characters
This helps sanitise user input strings and format output strings cleanly in Java.
Java String trim() Method
The trim() method in Java signature is:
public String trim()
It takes no parameters and returns a new String without leading or trailing whitespace from the original string.
Examples of trim() in Java
Let’s go through some examples to understand the working of trim():
Example 1: Removing leading and trailing spaces
This example demonstrates how the trim() method eliminates extra leading and trailing spaces in a string:
public class TrimExample {
public static void main(String[] args) {
String str = " Hello World ";
String trimmed = str.trim();
System.out.println("Original String: '" + str + "'");
System.out.println("Trimmed String: '" + trimmed + "'");
}
}
Output:
Original String: ‘ Hello World ‘
Trimmed String: ‘Hello World’
Example 2: Impact on original string
This example shows that trim() does not modify the original string:
public class TrimExample {
public static void main(String[] args) {
String str = " Welcome to Java ";
System.out.println("Before trim: " + str);
String newStr = str.trim();
System.out.println("After trim: " + newStr);
}
}
Output:
Before trim: Welcome to Java
After trim: Welcome to Java
Example 3: Comparing the original and the returned string
Let’s compare the original and returned string to see the difference:
public class TrimExample {
public static void main(String[] args) {
String str = " Hello World ";
System.out.println("Original: " + str);
String trimmed = str.trim();
System.out.println("Trimmed: " + trimmed);
boolean areEqual = str.equals(trimmed);
System.out.println("Original equals Trimmed: " + areEqual);
}
}
Output:
Original: Hello World
Trimmed: Hello World
Original equals Trimmed: false
Time Complexity: O(n)
Auxiliary Space: O(1)
Conclusion
In conclusion, the trim() method in Java is a valuable tool for handling and cleaning up strings, ensuring cleaner and more sanitised user input and formatted output. We’ve explored its purpose and usage and provided examples that demonstrate its functionality. Remember that the trim() method doesn’t affect spaces within the string itself, only the leading and trailing ones. It’s a simple yet powerful method that can make your Java code more robust and user-friendly.
So, the next time you encounter strings with unwanted whitespace, consider using the trim() method to tidy things up. Its time complexity is O(n), and it uses O(1) auxiliary space, making it an efficient choice for string manipulation in Java.
