Java Program to Remove All Whitespaces from a String
What is string in java?
In Java, a string is an object that represents a collection of characters. Because String objects in Java are immutable, their values cannot be modified once they have been created. Java has a number of ways to work with and modify Strings, including ways to concatenate, compare, search for, and extract substrings.
- Java provides the StringBuilder and StringBuffer classes for building Strings efficiently and dynamically.
To know more about Java Program to Remove ALL Whitespaces from a string read the complete article.
Steps to Java Program to Remove ALL Whitespaces from a string
- Create a string variable with the original string and all of the whitespaces.
- To eliminate all whitespace from the string, use the replaceAll() method from the String class. The procedure accepts a regular expression as an argument, where s matches any character that is a whitespace (space, tab, newline, etc.).
- Assign the returned value from the replaceAll() method to a new string variable.
- Print the new string without any whitespaces.
Strings In java :
Strings are immutable: once a string is created, its value cannot be changed. Instead, a new string must be created if the value needs to change.
Java Basic Input and Output :
The Scanner class can be used to read input from the user in the form of strings, integers, or other data types.
Let’s look at a Java Program to Remove ALL Whitespaces from a string is used to perform an operation.
Example 1: Java Program to Remove All Whitespaces
Run
public class Main{ public static void main(String[] args) { String str = "PrepInsta is a ed-tech company"; str = str.replaceAll("\\s", ""); System.out.println("String after removing whitespaces: " + str); } }
Output
String after removing whitespaces: PrepInstaisaed-techcompany
Explanation:
- The replaceAll method is used to replace all occurrences of the given regular expression (\\s) with an empty string. Any whitespace character, including spaces, tabs, and newlines, is matchable by the regular expressions.
Example 2 : Java program to Take string from users and remove the white space
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String inputString = sc.nextLine(); String withoutWhiteSpaces = inputString.replaceAll("\\s", ""); System.out.println("String without white spaces: " + withoutWhiteSpaces); } }
Output
Enter a string: PrepInsta and prepinsta prime String without white spaces: PrepInstaandprepinstaprime
Explanation:
This program uses the Scanner class to take input from the user, the replaceAll method to remove the white spaces, and the println method to print the output.
Example 3: Java Program to Remove All Whitespaces
Run
public class Main { public static void main(String[] args) { String sentence = "W h at i s p re p In s t a "; System.out.println("Original sentence: " + sentence); sentence = sentence.replaceAll("\\s", ""); System.out.println("After replacement: " + sentence); } }
Output
WhatisprepInsta
Explanation:
- This code creates a Java class called "Main" that has only the "main" function. The variable "sentence" is initially given the string "W h at I s p re p In s t a" via the technique. The value of "sentence" is then printed, followed by "Original sentence:".
- The "replaceAll" method and the regular expression "s" are then used to replace every whitespace character in the "sentence" string. Any character that is white space will match this expression. The "sentence" variable is then given a new string that is free of any whitespaces. The value of "sentence" is followed by "After replacement:" in the output.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment