Core Java

Different ways to remove Spaces from String In Java

String manipulation is done most often while programming. Like removing spaces in or around the string text. This also known as ‘strip’ping off spaces in the string. So up till now we are all aware of the different ways to remove spaces from string in java, namely trim, replaceAll. However, java 11 has made some new additions to these with methods like, strip, stripLeading , stripTrailing.

Majority of the times, we just use the trim method for removing spaces. We never stop and think is there may be a better way to suit our need? Sure, trim() works well for most of the cases, but there are many different methods in java. Each having its own advantages and disadvantages. How do we decide which method suits us best?

Well, in this blog we shall cover the different methods in detail.

Different ways to remove spaces from string in java

  1. trim() : Removing leading and trailing spaces from the string
  2. strip() : Removes spaces at the beginning and end of the string. Strip method is Unicode charset aware
  3. trim vs strip : Differences between trim and strip method
  4. stripLeading() : Removes white spaces only from the beginning of the string
  5. stripTrailing() : Removes white spaces only from ending of the string
  6. replace() : Replaces all target characters with new character
  7. replaceAll() : Replaces all matched characters with new character. This method takes regular expression as input to identify target substring that needs to be replaced
  8. replaceAll vs replace : Differences between replace and replaceAll method
  9. replaceFirst() : Replaces only first occurrence of target substring with new replacement string

Most important point to note is that in java a string object is immutable. It means we cannot modify a string, hence all the methods returns new string with all the transformations.

trim() method in java

trim() is the most commonly used method by java developers for removing leading and trailing spaces. For trim method space character means any character whose ASCII value is less than or equal to 32 (‘U+0020’).

Example of trim method to remove spaces:

1
2
3
4
5
6
7
8
public class StringTrimTest {
 
    public static void main(String[] args) {
        String string = "    String    with    space    ";
        System.out.println("Before trim: \"" + string +"\"");
        System.out.println("After trim: \"" + string.trim() +"\"");
   }
}

Output:

1
2
Before trim: "    String    with    space    "
After trim: "String    with    space"

strip() method Java 11

In the release of Java 11 new strip() method was added to remove leading and trailing spaces from String.

This method was added as there are various space characters according to Unicode standards having ASCII value more than 32(‘U+0020’). Ex: 8193(U+2001).

To identify these space characters, new method isWhitespace(int) was added from Java 1.5 in Character class. This method uses unicode to identify space characters. You can read more about unicode space characters here.

The strip method uses this Character.isWhitespace(int) method to cover wide range of white space characters and remove them.

Example of strip():

1
2
3
4
5
6
7
public class StringStripTest {
    public static void main(String[] args) {
        String string = "    String    with    space    ";
        System.out.println("Before strip: \"" + string+"\"");
        System.out.println("After strip: \"" + string.strip()+"\"");
    }
}

Output:

1
2
Before strip: "    String    with    space    "
After strip: "String    with    space"

Difference between trim and strip method in java

trim()strip()
From Java 1From Java 11
Uses codepoint(ASCII) valueUses Unicode value
Removes leading and trailing character(space)Removes leading and trailing character(space)
Removes characters having ASCII value less than or equal to ‘U+0020’ or ’32’Removes all space characters according to unicode

Let’s look at the example where we will use white space character higher than 32 (‘U+0020’) unicode.

1
2
3
4
5
6
7
8
public class StringTrimVsStripTest {
    public static void main(String[] args) {
        String string = '\u2001'+"String    with    space"+ '\u2001';
        System.out.println("Before: \"" + string+"\"");
        System.out.println("After trim: \"" + string.trim()+"\"");
        System.out.println("After strip: \"" + string.strip()+"\"");
   }
}

Output:

1
2
3
Before: "  String    with    space  "
After trim: " String    with    space "
After strip: "String    with    space"

In the above example we can see that trim method is unable to remove space character added by ‘\u2001’ unicode character.

Note: If you are running on windows machine, you may not be able to see the similar output due to limited unicode set. You can use online compilers to run program. Some online compiler links are as below,
Java-8: https://www.jdoodle.com/online-java-compiler/
Java-11: https://www.tutorialspoint.com/compile_java_online.php

stripLeading() method Java 11

Added in Java 11, stripLeading() method removes all leading spaces from a String.

Similar to strip method stripLeading also uses Character.isWhitespace(int) for identifyng white spaces.

1
2
3
4
5
6
7
public class StringStripLeadingTest {
    public static void main(String[] args) {
        String string = "    String    with    space    ";
        System.out.println("Before: \"" + string+"\"");
        System.out.println("After : \"" + string.stripLeading()+"\"");
    }
}

Output:

1
2
Before: "    String    with    space    "
After : "String    with    space    "

stripTrailing() method Java 11

Added in Java 11, stripTrailing() method removes all ending spaces from a String.

Similar to strip method stripTrailing also uses Character.isWhitespace(int) for identifying white spaces.

1
2
3
4
5
6
7
8
public class StringStripTrailingTest {
 
    public static void main(String[] args) {
      String string = "    String    with    space    ";
      System.out.println("Before: \"" + string+"\"");
        System.out.println("After : \"" + string.stripTrailing()+"\"");
    }
}

Output:

1
2
Before:"    String    with    space    "
After :"    String    with    space"

replace(CharSequence target, CharSequence replacement):

Added from java 1.5, This method is used to replace each target substring with the specified replacement string.

This method replaces all matching target elements.

Note: One more method replace(char oldChar, char newChar) is present in java string class. The only differences is that this method takes single character as target and replacement. We cannot use this method to remove space, because we cannot have empty character as replacement.

Example to remove all spaces from string

1
2
3
4
5
6
7
8
public class StringReplaceTest {
  
    public static void main(String[] args) {
        String string = "    String    with    space    ";
        System.out.println("Before : \"" + string + "\"");
        System.out.println("Replace: \"" + string.replace(" ", "") + "\"");
    }
}

Output:

1
2
Before  : "    String    with    space    "
Replace : "Stringwithspace"

replaceAll (String regex, String replacement)

Added in java 1.4, this is one of the most powerful method for string manipulation. We can use this method for many purposes.

Using replaceAll() method we can replace each matching regular expression substring with the given replacement string. For example for removing all spaces, removing leading spaces, removing trailing spaces and so on.

We just need to create correct regular expression with correct replacement parameter. Some regular expression examples as below:

\s+Find all space
^\s+Find all spaces at line beginning
\s+$Find all spaces at line ending

Example to replacing spaces in string,

NOTE: In java to add ‘/’ we have to use escape character so for “\s+” we have to use “\\s+”

01
02
03
04
05
06
07
08
09
10
public class StringReplaceAllTest {
    public static void main(String[] args) {
        String string = "    String    with    space    ";
        System.out.println("Before replaceAll : \"" + string+"\"");
        System.out.println("Replace all space : \"" + string.replaceAll(" ", "") + "\"");
        System.out.println("Replace all regex : \"" + string.replaceAll("\\s+", "") + "\"");
        System.out.println("Replace Leading   : \"" + string.replaceAll("^\\s+", "") + "\"");
        System.out.println("Replace trailing  : \"" + string.replaceAll("\\s+$", "") + "\"");
    }
}

Output:

1
2
3
4
5
Before replaceAll : "    String    with    space    "
Replace all space : "Stringwithspace"
Replace all regex : "Stringwithspace"
Replace Leading   : "String    with    space    "
Replace trailing  : "    String    with    space"

As we can see that replaceAll() is pretty powerful method if we use it with proper regular expression.

Difference between replaceAll and replace method

replaceAll()replace()
From Java 1.4From Java 1.5
Accepts regular expression for target identificationAccepts string for target identification
Used for fix or dynamic string replacementUsed for fix string replacement
Removes characters having ASCII value less than or equal to ‘U+0020’ or ’32’Removes all space characters according to unicode

replaceFirst(String regex, String replacement)

Added in java 1.4, replaceFirst method replaces only the first match of given regular expression with replacement string.

This method can be very useful if you just need to replace only one first occurence. For example if we just need to remove leading spaces we can use “\\s+” or “^\\s+”.

We can also use this method to remove trailing spaces by using “\\s+$” regular expression. As this expression will only match the last spaces in line. So last spaces are considered as the first match for this method.

Let’s take an example for removing leading and trailing spaces from string

1
2
3
4
5
6
7
8
public class StringReplaceFistTest {
      public static void main(String[] args) {
      String string = "    String    with    space    ";
      System.out.println("Before   : \"" + string+"\"");
        System.out.println("Replace  : \"" + string.replaceFirst("space", "update") + "\"");
        System.out.println("Leading  : \"" + string.replaceFirst("\\s+", "") + "\"");
        System.out.println("Trailing : \"" + string.replaceFirst("\\s+$", "") + "\"");    }
}

Output

1
2
3
4
Before   : "    String    with    space    "
Replace  : "    String    with    update    "
Leading  : "String    with    space    "
Trailing : "    String    with    space"

Fast track reading :

Related topics

Published on Java Code Geeks with permission by Stacktraceguru, partner at our JCG program. See the original article here: Different ways to remove Spaces from String In Java

Opinions expressed by Java Code Geeks contributors are their own.

Stacktraceguru

Stacktraceguru is an educational website. This website helps software programmers to learn and clarify concepts.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button