How to Remove Spaces From String In Java

How to Remove Spaces From String In Java | There are several methods to remove spaces in string Java. Let us demonstrate them one by one and understand them. Before getting into the demonstration let us see the below examples for more clarification. Examples:-

1) String = “Java Programming”
The string after removal of spaces:- “JavaProgramming”
2) String= “ Know Program “
The string after removal of spaces:- “KnowProgram”

Sometimes the string may have extra spaces which are called whitespace which is unnecessary hence the concept of removing spaces from a string helps to trim the unwanted spaces in the given string.

Remove Spaces From String In Java using replaceAll()

In this example, we will use the replaceAll() method to remove all the spaces in the string. In the replaceAll() method if we pass an empty string as a second parameter then it removes the first parameter from the given string.

public class Main {
   public static void main(String[] args) {
      String string = "   Know Program   ";
      string = string.replaceAll("\\s", "");
      System.out.println(string);
   }
}

Output:-

KnowProgram

Remove Spaces In String Java using trim()

Now we will use the trim() method to remove spaces. The trim() method removes only leading and trailing spaces from the given string but it doesn’t remove the spaces between words.

public class Main {
   public static void main(String[] args) {
      String str = "   Learn Java Programming Language   ";
      System.out.println("Before trim: \"" + str + "\"");
      System.out.println("After trim: \"" + str.trim() + "\"");
   }
}

Output:-

Before trim: ” Learn Java Programming Language “
After trim: “Learn Java Programming Language”

String Remove Spaces Java using strip()

The below demonstration is by using the strip() method. The strip() method is very similar to the trim() method, and it was introduced in Java 11 version. It returns a string whose value is this string, with all leading and trailing white space removed.

public class Main {
   public static void main(String[] args) {
      String str = "    Java Programming Language   ";
      System.out.println("Before strip: \"" + str + "\"");
      System.out.println("After strip: \"" + str.strip() + "\"");
   }
}

Output:

Before strip: ” Java Programming Language “
After strip: “Java Programming Language”

In the below code demonstration we show the difference between the strip() and trim() method of the Java String class.

public class Test {
   public static void main(String[] args) {
      String str1 = 
        '\u2001'+"Jav a   Programmin   g  Language"+ '\u2001';
      String str2 = 
        '\u2001'+"Jav a   Programmin   g  Language"+ '\u2001';

      System.out.println("Before: \"" + str1 + "\"");
      System.out.println("After trim: \"" + str1.trim() + "\"");
      System.out.println("After strip: \"" + str2.strip() + "\"");
   }
}

Output:-

Before: “?Jav a Programmin g Language?”
After trim: “?Jav a Programmin g Language?”
After strip: “Jav a Programmin g Language”

How To Remove Leading Space From String In Java

Now, let us demonstrate the stripLeading(). The stripLeading() method removes only the leading spaces from the given string.

public class Main {
   public static void main(String[] args) {
      String str = "    Java Programming Language    ";
      System.out.println("Before: \"" + str + "\"");
      System.out.println("After : \"" + str.stripLeading() + "\"");
   }
}

Output:

Before: " Java Programming Language "
After : "Java Programming Language "

How to Remove Trailing Space From a String In Java

The stripTraling() method removes the spaces only in the end that is at the trailing. 

public class Main {
   public static void main(String[] args) {
      String str = "    Java Programming Language    ";
      System.out.println("Before: \"" + str + "\"");
      System.out.println("After : \"" + str.stripTrailing() + "\"");
   }
}

Output:

Before: " Java Programming Language "
After : " Java Programming Language"

We can say that trim() method of Java String class is combination of the stripLeading() method and stripTraling() method. The stripLeading() method removes only leading whitespace, stripTraling() method removes only trailing whitespace. Whereas the trim() method removes both leading and trailing whitespace.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.