Java - Remove White Spaces from String using Regex Last Updated : 26 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 using Regex. PrerequisitesBasic of Java ProgrammingRegex patterns in JavaRemove White Spaces Using RegexHere \\s is the shorthand character class which is used to represent any white space characters in the string. And It can include spaces, tabs, line breaks also. The + is the Qualifier which indicates more than one white space. Finally, the Required Regular Expression is \\s+. Below is the implementation of the above-mentioned regex: Java // Java Program to remove // white spaces using regex import java.util.regex.*; // Main Class public class RegexWhiteSpaceExampleOne { // Main method public static void main(String[] args) { // Input String String inputString = " Welcome to GeeksForGeeks "; // Remove white spaces using regex String outputString = inputString.replaceAll("\\s+", ""); // Display the results System.out.println("Input String: \"" + inputString + "\""); System.out.println("Output String: \"" + outputString + "\""); } } Output:Explaination of the above Program:In the above code First I take one String value which is already explained in the above after that I use replaceAll String method. To this replaceAll method we pass the regex '\s+'. I already explain this regular expression in above you can read it once again if you don't understand this regex pattern. After that print the inputString value and outputString value. Comment More infoAdvertise with us Next Article How to Remove all White Spaces from a String in Java? E eswarbe06sp Follow Improve Article Tags : Java Java Programs Java-Strings java-regular-expression regular-expression Java Examples +2 More Practice Tags : JavaJava-Strings Similar Reads How to Remove all White Spaces from a String in Java? 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 remov 3 min read How to Remove Duplicates from a String in Java Using Regex ? In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in J 2 min read Java Program to Trim Leading and Trailing Spaces from a String In Java, the trim() method is used to remove leading and trailing spaces from a string. This method does not remove spaces between words but it eliminates unnecessary spaces at the beginning and end of the string. The trim() method is defined under the String class in the java.lang package. It check 1 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 How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl 2 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 Like