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
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.
Trimming Spaces in Java String:
trim()
method removes leading and trailing spaces.String trimmed = inputString.trim();
Remove Leading and Trailing Spaces in Java:
trim()
to remove both leading and trailing spaces.String trimmed = inputString.trim();
Java String Replace Spaces:
replace()
to replace spaces with another character or an empty string.String replaced = inputString.replace(" ", "_");
Removing Whitespace Characters in Java:
String removedWhitespace = inputString.replaceAll("\\s", "");
Using String.replaceAll()
to Remove Spaces in Java:
replaceAll()
with a regex.String noSpaces = inputString.replaceAll("\\s", "");
Trimming Multiple Spaces in Java:
String trimmedMultipleSpaces = inputString.replaceAll("\\s+", " ");
Removing All Whitespace from a String in Java:
String noWhitespace = inputString.replaceAll("\\s", "");
Java Regex to Remove Spaces from String:
String noSpacesRegex = inputString.replaceAll("\\s", "");
Java StringTokenizer
to Remove Spaces:
StringTokenizer tokenizer = new StringTokenizer(inputString, " "); String noSpaces = String.join("", Collections.list(tokenizer.elements()));
Replacing Spaces with Underscores in Java:
replace()
.String underscores = inputString.replace(" ", "_");
Removing Spaces and Special Characters in Java:
String noSpacesOrSpecialChars = inputString.replaceAll("[^a-zA-Z0-9]", "");
Java 8+ Ways to Remove Spaces from String:
replaceAll()
with regex for whitespace removal.String noSpacesJava8 = inputString.replaceAll("\\s", "");
Handling Whitespace in Java String Manipulation:
String manipulatedString = inputString.replaceAll("\\s", "");
Common Mistakes When Removing Spaces in Java:
// Incorrect inputString.replaceAll("\\s", ""); // Correct inputString = inputString.replaceAll("\\s", "");