Remove leading and trailing whitespace in Java (left trim and right trim)
This post will discuss how to remove leading and trailing whitespace in Java. In ASCII, whitespace characters are space (' '), tab ('\t'), carriage return ('\r'), newline ('\n'), vertical tab ('\v') and form feed ('\f').
1. Using String.replaceAll() method
We can use of String class replaceAll() method to implement left and right trim in Java. The replaceAll() method takes a regular expression and replaces each substring of this string that matches the regex with the specified replacement.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Main { private static final String EMPTY_STRING = ""; public static String ltrim(String str) { return str.replaceAll("^\\s+", EMPTY_STRING); } public static String rtrim(String str) { return str.replaceAll("\\s+$", EMPTY_STRING); } public static void main(String[] args) { String str = " Hello World "; System.out.println("Left Trim :" + ltrim(str) +":"); System.out.println("Right Trim :" + rtrim(str) +":"); } } |
Output:
Left Trim :Hello World :
Right Trim: Hello World:
For better performance, we recommend compiling the regular expression first, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.regex.Pattern; class Main { private static final Pattern LTRIM = Pattern.compile("^\\s+"); private static final Pattern RTRIM = Pattern.compile("\\s+$"); private static final String EMPTY_STRING = ""; public static String ltrim(String str) { return LTRIM.matcher(str).replaceAll(EMPTY_STRING); } public static String rtrim(String str) { return RTRIM.matcher(str).replaceAll(EMPTY_STRING); } public static void main(String[] args) { String str = " Hello World "; System.out.println("Left Trim :" + ltrim(str) +":"); System.out.println("Right Trim :" + rtrim(str) +":"); } } |
Output:
Left Trim :Hello World :
Right Trim: Hello World:
2. Naive solution
We can even write our own utility methods, which doesn’t involve using any regex and performs comparatively faster.
|
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 27 28 |
class Main { public static String ltrim(String str) { int i = 0; while (i < str.length() && Character.isWhitespace(str.charAt(i))) { i++; } return str.substring(i); } public static String rtrim(String str) { int i = str.length() - 1; while (i >= 0 && Character.isWhitespace(str.charAt(i))) { i--; } return str.substring(0, i + 1); } public static void main(String[] args) { String str = " Hello World "; System.out.println("Left Trim :" + ltrim(str) +":"); System.out.println("Right Trim :" + rtrim(str) +":"); } } |
Output:
Left Trim :Hello World :
Right Trim: Hello World:
3. Using Apache Commons Lang
Finally, we can leverage Apache Commons Lang StringUtils class stripStart() and stripEnd() utility methods, which can strip the specified set of whitespace characters from the start and end of a string, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.apache.commons.lang3.StringUtils; class Main { public static String ltrim(String str) { return StringUtils.stripStart(str, " \t\r\n\\v\f"); } public static String rtrim(String str) { return StringUtils.stripEnd(str, " \t\r\n\\v\f"); } public static void main(String[] args) { String str = " Hello World "; System.out.println("Left Trim :" + ltrim(str) +":"); System.out.println("Right Trim :" + rtrim(str) +":"); } } |
That’s all about removing leading and trailing whitespace 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 :)