Implement trim() method in Java
This post will discuss how to implement the trim() method in Java.
The trim() method in Java is a built-in method provided by the String class that removes leading and trailing spaces from a string. It checks for whitespace characters before and after the string, and if it exists, it removes it and returns the modified string. To implement the trim() method in Java, or to achieve a similar functionality, we can use one of the following methods:
1. Using String.replaceAll() method
We can use the String class replaceAll() method to implement the trim() method in Java. The replaceAll() method takes a regular expression and replaces each substring of this string that matches the regex with the specified replacement. To perform the trim operation, we can use the regex "^\s+|\s+$" to match with the leading and trailing spaces, and replace them with an empty string. Since String is immutable in Java, we can’t perform trim operation in-place. To change the original string, we might need to assign the trimmed string back to the original variable. For example:
|
1 2 3 4 5 6 7 8 9 |
class Main { public static void main(String[] args) { String str = " \tTrim Me "; str = str.replaceAll("^\\s+|\\s+$", ""); System.out.println("\"" + str + "\""); // "Trim Me" } } |
2. Using a loop
We can even write our own utility methods, which doesn’t involve using any regex. The idea is to use a loop to iterate over the string from left to right and find the index of the first non-whitespace character. Similary, use another loop to iterate over the string from right to left, and find the index of the last non-whitespace character. Then use the substring() method to return a copy of the string between those indices. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class Main { public static String trim(String s) { int i = 0, j = s.length() - 1; // Find the first non-whitespace character from left while (i <= j && Character.isWhitespace(s.charAt(i))) { i++; } // Find the last non-whitespace character from right while (j >= i && Character.isWhitespace(s.charAt(j))) { j--; } // Return a substring from i to j + 1 return s.substring(i, j + 1); } public static void main(String[] args) { String str = " \tTrim Me "; str = trim(str); System.out.println("\"" + str + "\""); // "Trim Me" } } |
3. Using Apache Commons Lang
The StringUtils class from Apache Commons Lang library provides various utility methods for manipulating strings, including a trim() method that behaves exactly like the trim() method of the String class. It strips whitespace from the start and end of a string. To use it, we need to import the org.apache.commons.lang3.StringUtils class and then call it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String str = " \tTrim Me "; str = StringUtils.strip(str); System.out.println("\"" + str + "\""); // "Trim Me" } } |
4. Using Guava class
Finally, we can leverage Guava CharMatcher class that has several methods for matching and manipulating characters in strings. It provides a trimFrom() method that removes leading and trailing characters from a string which satisfy a given condition. To use this method, we need to include the guava jar in the project, and then invoke this method as follows:
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.base.CharMatcher; class Main { public static void main(String[] args) { String str = " \tTrim Me "; str = CharMatcher.whitespace().trimFrom(str); System.out.println("\"" + str + "\""); // "Trim Me" } } |
That’s all about implementing the trim() method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)