How to Remove all White Spaces from a String in Java? Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remove all whitespaces from a string. This is the most efficient and concise method. Java // Java program to remove all white spaces // from a string using replaceAll() method class BlankSpace { public static void main(String[] args) { String s = " Geeks for Geeks "; // Call the replaceAll() method to remove all white spaces s = s.replaceAll("\\s", ""); System.out.println(s); } } OutputGeeksforGeeks Table of ContentOther Methods to Remove White Spaces from a String Using Character Class Built-in FunctionUsing String.replace() MethodUsing Java 8 StreamsOther Methods to Remove White Spaces from a String Apart from the simple method mentioned above, there are few more methods available to remove all white spaces from a string in Java.1. Using Character Class Built-in FunctionIn this case, we are going to declare a new string and then simply add every character one by one ignoring the white space. For this, we will use Character.isWhitespace(c).Example: Java // Java program to demonstrate how to remove all white spaces from a string import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { String s = " Geeks for Geeks "; String a = ""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); // Checking whether is white space or not if (!Character.isWhitespace(c)) { a += c; } } System.out.println(a); } } OutputGeeksforGeeks 2. Using String.replace() MethodThe replace() method replaces all occurrences of a space (" ") in the string with an empty string ("") by effectively removing all white spaces.Example: Java // Java program to demonstrate how to remove all white spaces from a string // using the String.replace() method import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { String s = " Geeks for Geeks "; String a = s.replace(" ",""); System.out.println(a); } } OutputGeeksforGeeks 3. Using Java 8 StreamsJava 8 introduced Streams, which can also be used to remove white spaces by filtering characters.Example: Java // Java program to demonstrate how to remove all white spaces from a string // using Java 8 Streams import java.io.*; import java.util.*; import java.util.stream.Collectors; class GFG { public static void main(String[] args) { String s = " Geeks for Geeks "; String a = s.chars() .filter(c -> !Character.isWhitespace(c)) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println(a); } } OutputGeeksforGeeks Comment More infoAdvertise with us Next Article How to Remove all White Spaces from a String in Java? bilal-hungund Follow Improve Article Tags : Strings Java Technical Scripter Java Programs DSA Java-Strings +2 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Remove Duplicates from a String in Java? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a 2 min read Java - Remove White Spaces from String using Regex The Regex is an expression that is used for searching a required pattern or matching a required pattern or Manipulating the input based on requirements by using regex. This Regex is available in java.util.regex.* package in Java Programming. In this article, we will learn to remove the white spaces 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read Java Program to Remove a Given Word From a String Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output. For more clarity go through the illustration given below as follows. Illustration: Input : This is the Geeks For Geeks word="the" Output : This is Geeks For Geeks Input : Hello world 3 min read How to Split a String in Java with Delimiter? In Java, the split() method of the String class is used to split a string into an array of substrings based on a specified delimiter. The best example of this is CSV (Comma Separated Values) files.Program to Split a String with Delimiter in JavaHere is a simple program to split a string with a comma 2 min read Like