Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Remove Spaces From String

Removing spaces from a string in Java can be done using various methods. In this tutorial, we will cover two common methods to remove spaces from a string: using the replace() method and using regular expressions.

Method 1: Using the replace() method

The replace() method of the String class can be used to replace all occurrences of a specific character with another character. To remove spaces, you can replace all space characters with an empty character.

Example:

public class Main {
    public static void main(String[] args) {
        String input = "Hello World! This is an example.";
        String output = input.replace(" ", "");
        System.out.println("Original string: " + input);
        System.out.println("String without spaces: " + output);
    }
}

Method 2: Using regular expressions

Java provides support for regular expressions through the java.util.regex package. The replaceAll() method of the String class can be used with a regular expression pattern to replace all occurrences of a pattern with a specified string. In this case, we will replace all space characters with an empty string.

Example:

public class Main {
    public static void main(String[] args) {
        String input = "Hello World! This is an example.";
        String output = input.replaceAll("\\s+", "");
        System.out.println("Original string: " + input);
        System.out.println("String without spaces: " + output);
    }
}

In this example, the regular expression pattern \\s+ matches one or more space characters (including tabs and line breaks). The replaceAll() method replaces all occurrences of the pattern with an empty string.

In this tutorial, we covered two methods to remove spaces from a string in Java. Both methods can be used depending on your preference or the specific requirements of your application.

  1. Trimming Spaces in Java String:

    • The trim() method removes leading and trailing spaces.
    String trimmed = inputString.trim();
    
  2. Remove Leading and Trailing Spaces in Java:

    • Using trim() to remove both leading and trailing spaces.
    String trimmed = inputString.trim();
    
  3. Java String Replace Spaces:

    • Using replace() to replace spaces with another character or an empty string.
    String replaced = inputString.replace(" ", "_");
    
  4. Removing Whitespace Characters in Java:

    • Using regex to replace all whitespace characters.
    String removedWhitespace = inputString.replaceAll("\\s", "");
    
  5. Using String.replaceAll() to Remove Spaces in Java:

    • Removing spaces using replaceAll() with a regex.
    String noSpaces = inputString.replaceAll("\\s", "");
    
  6. Trimming Multiple Spaces in Java:

    • Using regex to trim multiple consecutive spaces.
    String trimmedMultipleSpaces = inputString.replaceAll("\\s+", " ");
    
  7. Removing All Whitespace from a String in Java:

    • Eliminating all whitespace characters.
    String noWhitespace = inputString.replaceAll("\\s", "");
    
  8. Java Regex to Remove Spaces from String:

    • Using a regex pattern to remove spaces.
    String noSpacesRegex = inputString.replaceAll("\\s", "");
    
  9. Java StringTokenizer to Remove Spaces:

    • Tokenizing and joining to remove spaces.
    StringTokenizer tokenizer = new StringTokenizer(inputString, " ");
    String noSpaces = String.join("", Collections.list(tokenizer.elements()));
    
  10. Replacing Spaces with Underscores in Java:

    • Replacing spaces with underscores using replace().
    String underscores = inputString.replace(" ", "_");
    
  11. Removing Spaces and Special Characters in Java:

    • Using regex to remove spaces and special characters.
    String noSpacesOrSpecialChars = inputString.replaceAll("[^a-zA-Z0-9]", "");
    
  12. Java 8+ Ways to Remove Spaces from String:

    • Utilizing Java 8 replaceAll() with regex for whitespace removal.
    String noSpacesJava8 = inputString.replaceAll("\\s", "");
    
  13. Handling Whitespace in Java String Manipulation:

    • Consideration of whitespace variations (spaces, tabs, newlines) when manipulating strings.
    String manipulatedString = inputString.replaceAll("\\s", "");
    
  14. Common Mistakes When Removing Spaces in Java:

    • Avoid common pitfalls like not assigning the result back to the original string.
    // Incorrect
    inputString.replaceAll("\\s", "");
    
    // Correct
    inputString = inputString.replaceAll("\\s", "");