
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to replace multiple spaces in a string using a single space using Java regex?
The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.
Match the input string with the above regular expression and replace the results with single space “ ”.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAllExample { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "\s+"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); //Replacing all space characters with single space String result = matcher.replaceAll(" "); System.out.print("Text after removing unwanted spaces: \n"+result); } }
Output
Enter a String hello this is a sample text with irregular spaces Text after removing unwanted spaces: hello this is a sample text with irregular spaces
Example 2
import java.util.Scanner; public class Test { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match space(s) String regex = "\s+"; //Replacing the pattern with single space String result = input.replaceAll(regex, " "); System.out.print("Text after removing unwanted spaces: \n"+result); } }
Output
Enter a String hello this is a sample text with irregular spaces Text after removing unwanted spaces: hello this is a sample text with irregular spaces
- Related Articles
- How to replace multiple spaces with a single space in C#?
- How to match a white space equivalent using Java RegEx?
- How to match a non-white space equivalent using Java RegEx?
- How to remove white spaces using Java Regular Expression (RegEx)
- Replace Character in a String in Java without using replace() method
- How match a string irrespective of case using Java regex.
- How to extract an HTML tag from a String using regex in Java?
- How to match end of a particular string/line using Java RegEx
- How to match beginning of a particular string/line using Java RegEx
- How to replace Digits into String using Java?
- Extracting each word from a string using Regex in Java
- How to check multiple regex patterns against an input? Using Java.
- Java Program to Replace the Spaces of a String with a Specific Character
- Java regex program to split a string at every space and punctuation.
- How to match a character from given string including case using Java regex?

Advertisements