- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to remove all the white spaces from a given string
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with ".
Example
import java.io.*; import java.util.Scanner; public class RemovingWhiteSpaces { public static void main(String args[]) { System.out.println("Enter a string value ::"); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println(str.replaceAll(" ", "")); } }
Output
Enter a string value :: Hi welcome to tutorialspoint Hiwelcometotutorialspoint
- Related Articles
- Java Program to remove all white spaces from a String.
- C++ Program to remove spaces from a string?
- Java Program to Remove All Whitespaces from a String
- C++ program to remove spaces from a string using String stream
- How to remove white spaces (leading and trailing) from string value in MongoDB?
- Java program to remove all duplicates words from a given sentence
- How to remove white spaces using Java Regular Expression (RegEx)
- How to remove all special characters, punctuation and spaces from a string in Python?
- Golang program to remove all whitespaces from a string
- Remove all duplicates from a given string in C#
- Remove all duplicates from a given string in Python
- Remove spaces from std::string in C++
- C program to remove extra spaces using string concepts.
- Removing all spaces from a string using JavaScript
- C program to remove spaces in a sentence using string concepts.

Advertisements