String is one of the most commonly used data types in Java. We often use strings to do a lot of things in our daily development. Such as string concatenation, truncation, substitution, etc.

In this article, we introduce a relatively common and overlooked operation that removes whitespace from a string.

There are many different ways to remove whitespace from strings in Java, such as trim, replaceAll, and so on. However, new features were added in Java 11 such as Strip, stripLeading, stripTrailing, and more.

Most of the time, we just use the trim method to remove excess whitespace. But a lot of people don’t seem to think about, is there a better way?

Of course, trim() works fine in most cases, but there are many different approaches in Java. Each has its own advantages and disadvantages. How do we decide which approach works best for us?

Next, we will introduce several methods and compare their differences, advantages and disadvantages.

Different ways to remove whitespace from a string in Java

First, let’s take a look at how many ways you can remove whitespace from a String. Based on our experience, we’ve come up with the following seven methods:

  • Trim () : Removes the leading and trailing Spaces from the string.
  • Strip () : Removes Spaces at the beginning and end of the string.
  • StripLeading () : Remove only the header of the string
  • StripTrailing () : Removes only the trailing space of the string
  • Replace () : Replaces all target characters with new characters
  • ReplaceAll () : Replaces all matched characters with new characters. This method takes a regular expression as input to identify the target substring that needs to be replaced
  • ReplaceFirst () : Replaces only the first occurrence of the target substring with a new string

The most important thing to note is that strings are immutable in Java, which means we can’t modify strings, so all we get is a new String.

Next, we respectively study the usage of these methods and understand their characteristics.

PS: The code in this article is run online (www.jdoodle.com/online-java…) Executed because Java 11 is not installed on my test machine and Unicode characters are incomplete. If you want to experiment, you are advised to use the online tool, select the corresponding JDK.

trim

Trim () is the most common method used by Java developers to remove whitespace at the beginning and end of strings. Its usage is also relatively simple:

public class StringTest { public static void main(String[] args) { String stringWithSpace = " Hollis Is A Java Coder "; StringTest.trimTest(stringWithSpace); } private static void trimTest(String stringWithSpace){ System.out.println("Before trim : \'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.trim(); System.out.println("After trim : \'" + stringAfterTrim + "\'"); }}Copy the code

Output result:

Before trim : '   Hollis   Is   A   Java   Coder   '
After trim : 'Hollis   Is   A   Java   Coder'
Copy the code

As shown above, using trim removes the leading and trailing whitespace from the string.

But have you ever wondered what the trim method removes from the whitespace contains? Are there any other characters besides Spaces?

In fact, trim removes whitespace for any character with an ASCII value of 32 or less (‘ U+0020 ‘) :

It contains Spaces, newlines, backspace, and other characters.

strip()

I don’t know if you’ve noticed, but in the Java 11 release, a new strip() method was added to remove leading and trailing Spaces from strings.

Why add a strip when you already have a trim method?

This is because the trim method can only remove characters with an ASCII value of 32 or less, but according to the Unicode standard, there are many whitespace characters other than ASCII characters.

And to recognize these Spaces, starting with Java 1.5, a new isWhitespace(int) method was added to the Character class. This method uses Unicode to identify space characters. You can find it at jkorpela.fi/chars/ Space… Learn more about Unicode space characters.

The new strip method in Java 11 uses the character.iswhitespace (int) method to determine if whitespace characters are present and delete them:

Let’s look at an example using strip:

public class StringTest {
    public static void main(String args[]) {
      String stringWithSpace ='\u2001' + "  Hollis   Is   A   Java   Coder  " + '\u2001';
        System.out.println("'" + '\u2001' + "' is space : " +  Character.isWhitespace('\u2001'));
        StringTest.stripTest(stringWithSpace);
    }

    private static void stripTest(String stringWithSpace){
        System.out.println("Before strip : \'" + stringWithSpace + "\'");
        String stringAfterTrim = stringWithSpace.strip();
        System.out.println("After strip : \'" + stringAfterTrim + "\'");
    }
}
Copy the code

We add a special Character \u2001 before and after the string. This Character is not in ASCII and is judged to be a blank Character by character. isWhitespace. Strip is then used for processing, and the output is as follows:

'is space: true Before Strip: 'Hollis is A Java Coder' After Strip: 'Hollis is A Java Coder'Copy the code

As a result, the Strip method in Java 11 is more powerful than the trim method, which can remove many whitespace characters that are not in ASCII through the character.iswhitespace method.

Difference between trim and strip methods

We introduced the above two can remove the string at the beginning and the end of the method, the trim and strip, and then to compare their difference:

| trim | strip | | — – | — – | a Java 1 introduction of 11 introduced use ASCII | | Java USES Unicode value for the beginning and end of blank characters | delete the beginning and end of blank characters Delete the ASCII value less than or equal to ‘U + 0020’ or ’32 ‘according to unicode characters | delete all whitespace characters

StripLeading () and stripTrailing ()

StripLeading () and stripTrailing() methods were also added in Java 11. Delete the space at the beginning of the string and the space at the end of the string.

Similar to the strip method, stripTrailing also uses character.iswhitespace (int) to identify blank characters. The usage is similar to strip:

public class StringTest {
    public static void main(String args[]) {
      String stringWithSpace ='\u2001' + "  Hollis   Is   A   Java   Coder  " + '\u2001';
        System.out.println("'" + '\u2001' + "' is space : " +  Character.isWhitespace('\u2001'));
        StringTest.stripLeadingTest(stringWithSpace);
        StringTest.stripTrailingTest(stringWithSpace);
    }

    private static void stripLeadingTest(String stringWithSpace){
        System.out.println("Before stripLeading : \'" + stringWithSpace + "\'");
        String stringAfterTrim = stringWithSpace.stripLeading();
        System.out.println("After stripLeading : \'" + stringAfterTrim + "\'");
    }

     private static void stripTrailingTest(String stringWithSpace){
        System.out.println("Before stripTrailing : \'" + stringWithSpace + "\'");
        String stringAfterTrim = stringWithSpace.stripTrailing();
        System.out.println("After stripTrailing : \'" + stringAfterTrim + "\'");
    }
}
Copy the code

Output result:

"Is space: true Before stripLeading: 'Hollis is A Java programmer' After stripLeading: 'Hollis Is A Java Coder 'Before stripTrailing: 'Hollis Is A Java Coder' After stripTrailing: 'Hollis Is A Java Coder'Copy the code

replace

Another way to remove whitespace from a string, in addition to trim or strip, is to use the replace method to replace whitespace.

Replace was added from Java 1.5 to replace each target substring with a specified string.

This method replaces all matched target elements as follows:

public class StringTest { public static void main(String args[]) { String stringWithSpace =" Hollis Is A Java Coder "; StringTest.replaceTest(stringWithSpace); } private static void replaceTest(String stringWithSpace){ System.out.println("Before replace : \'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.replace(" ", ""); System.out.println("After replace : \'" + stringAfterTrim + "\'"); }}Copy the code

Results:

Before replace : '  Hollis   Is   A   Java   Coder  '
After replace : 'HollisIsAJavaCoder'
Copy the code

As you can see, the replace method replaces all whitespace characters in the string. In particular, the replace method, like the trim method, can only replace whitespace characters in ASCII.

replaceAll

ReplaceAll is one of the most powerful string manipulation methods added in Java 1.4. We can use this method for many purposes.

Using the replaceAll() method, we can use regular expressions to identify the target character content that needs to be replaced. With regular expressions, you can delete all Spaces, start Spaces, end Spaces, and so on.

We just need to create the correct regular expression with the correct substitution arguments. Some examples of regular expressions are as follows:

\s+ all whitespace characters ^\s+ all whitespace characters at the beginning of string \ S + all whitespace characters at the end of string $Copy the code

Note that to add/in Java we must use the escape character, so for \s+ we must use \\s+

public class StringTest { public static void main(String args[]) { String stringWithSpace =" Hollis Is A Java Coder "; StringTest.replaceAllTest(stringWithSpace," "); StringTest.replaceAllTest(stringWithSpace,"\\s+"); StringTest.replaceAllTest(stringWithSpace,"^\\s+"); StringTest.replaceAllTest(stringWithSpace,"\\s+$"); } private static void replaceAllTest(String stringWithSpace,String regex){ System.out.println("Before replaceAll with '"+ regex +"': \'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.replaceAll(regex, ""); System.out.println("After replaceAll with '"+ regex +"': \'" + stringAfterTrim + "\'"); }}Copy the code

Results:

Before replaceAll with ' ': '  Hollis   Is   A   Java   Coder  '
After replaceAll with ' ': 'HollisIsAJavaCoder'
Before replaceAll with '\s+': '  Hollis   Is   A   Java   Coder  '
After replaceAll with '\s+': 'HollisIsAJavaCoder'
Before replaceAll with '^\s+': '  Hollis   Is   A   Java   Coder  '
After replaceAll with '^\s+': 'Hollis   Is   A   Java   Coder  '
Before replaceAll with '\s+$': '  Hollis   Is   A   Java   Coder  '
After replaceAll with '\s+$': '  Hollis   Is   A   Java   Coder'
Copy the code

As we’ve seen, replaceAll() can be a very powerful method if used with appropriate regular expressions.

replaceFirst

The replaceFirst method, also added in Java 1.4, simply replaces the first match of a given regular expression with a replacement string.

This is useful if you only need to replace the first occurrence. For example, if we only needed to remove leading Spaces, we could use \\s+ or ^\\s+.

We can also use this method to remove trailing whitespace by using the \\s+$regular expression. Because this expression will only match the last space of the row. So the last space is considered the first match for this method.

Let’s take an example of removing leading and trailing whitespace from a string

public class StringTest { public static void main(String args[]) { String stringWithSpace =" Hollis Is A Java Coder "; StringTest.replaceFirstTest(stringWithSpace," "); StringTest.replaceFirstTest(stringWithSpace,"\\s+"); StringTest.replaceFirstTest(stringWithSpace,"^\\s+"); StringTest.replaceFirstTest(stringWithSpace,"\\s+$"); } private static void replaceFirstTest(String stringWithSpace,String regex){ System.out.println("Before replaceFirst with '"+ regex +"': \'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.replaceFirst(regex, ""); System.out.println("After replaceFirst with '"+ regex +"': \'" + stringAfterTrim + "\'"); }}Copy the code

Results:

Before replaceFirst with ' ': '  Hollis   Is   A   Java   Coder  '
After replaceFirst with ' ': ' Hollis   Is   A   Java   Coder  '
Before replaceFirst with '\s+': '  Hollis   Is   A   Java   Coder  '
After replaceFirst with '\s+': 'Hollis   Is   A   Java   Coder  '
Before replaceFirst with '^\s+': '  Hollis   Is   A   Java   Coder  '
After replaceFirst with '^\s+': 'Hollis   Is   A   Java   Coder  '
Before replaceFirst with '\s+$': '  Hollis   Is   A   Java   Coder  '
After replaceFirst with '\s+$': '  Hollis   Is   A   Java   Coder'
Copy the code

conclusion

This article describes seven ways to remove whitespace from a string.

To remove the start of the string directly, stripLeading, replaceAll, and replaceFirst

To remove whitespace from the end of a string directly, use stripTrailing, replaceAll, and replaceFirst

To remove whitespace from both the beginning and end of a string, use strip, trim

To remove all whitespace from a string, use replace and replaceAll

The 11 new Java strip, stripTrailing and stripLeading methods remove more characters than the effort effort. They remove whitespace not only from ASCII but from all Unicode whitespace. You can use character. isWhitespace to determine the specific method.