June 29, 2022

Remove Spaces From a String in Java - trim(), strip()

In this post you’ll learn how to remove spaces from a String in Java, the spaces may be leading spaces, trailing spaces or spaces in between the words. String class provides following options for removing String spaces in Java.

  • If you want to remove spaces at the beginning (leading spaces) and spaces at the end (trailing spaces) best way to do it is to use trim() method of the Java String class. As per trim() method space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
  • Java 11 onward there is also strip() method in Java String class for removing leading and trailing white spaces. This method internally uses the Character.isWhitespace() to check for whitepsaces. There are two more methods for removing either leading spaces or trailing spaces.

    If you want to remove only trailing white spaces then you can use-

    • stripLeading()- To remove all leading white spaces. Available Java 11 onward.

    If you want to remove only leading white spaces then you can use-

    • stripTrailing()- To remove all trailing white spaces. Available Java 11 onward.
  • Another option is to use replaceAll() method of the Java String class, passing regex ‘\\s+’ as an argument for removing spaces. This option removes spaces in between the words too apart from leading and trailing spaces.

One thing to remember here is that whenever string is modified in anyway a new String is created as String is immutable in Java, so you need to assign the modified string to a String reference.

Removing spaces using trim() method Java example

  • trim()- Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).
public class StringSpaces {
  public static void main(String[] args) {	
    String str = "  Hello    World		";
    str = str.trim();
    System.out.println("String- " + str);
  }
}
Output
String- Hello    World

As you can see leading and trailing spaces are removed though not the spaces in between the words.

Removing spaces using strip() method Java example

Java 11 onward there is also a strip() method in Java to remove leading and trailing spaces from a String. strip() method internally uses Character.isWhitespace() to check for white spaces which provides a much wider definition of whitespaces than trim(). In trim() space is defined as any character whose codepoint is less than or equal to 'U+0020' (the space character).

Let’s try to clarify it with an example where a unicode character '\u2002' is used which is not recognized by trim() method.

public class StringSpaces {
  public static void main(String[] args) {
    String str = '\u2002'+"Hello World!"+ '\u2002';
    System.out.println("String-" + str);
    str = str.trim();
    System.out.println("String-" + str);
  }
}
Output
String- Hello World!
String- Hello World!

You can see that the leading and trailing spaces are not removed by the trim() method as unicode character greater than '\u0020' is used. Using strip() method you can remove these spaces.

public class StringSpaces {
  public static void main(String[] args) {
    String str = '\u2002'+"Hello World!"+ '\u2002';
    System.out.println("String-" + str);
    str = str.strip();
    System.out.println("String-" + str);
  }
}
Output
String- Hello World!
String-Hello World!

Removing spaces using replaceAll() method Java example

If you have to remove spaces in between the words too apart from leading and trailing spaces then replaceAll() method can be used.

  • replaceAll(String regex, String replacement)- Replaces each substring of this string that matches the given regular expression with the given replacement.

By passing "\\s+" as regex which matches one or many whitespaces and "" as replacement for those whitespaces you can remove all whitespaces from a String.

public class StringSpaces {
  public static void main(String[] args) {	
    String str = "  Hello    World		";
    str = str.replaceAll("\\s+", "");
    System.out.println("String- " + str);
  }
}
Output
String- Hello World!
String- HelloWorld

That's all for the topic Remove Spaces From a String in Java - trim(), strip() Methods. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment