Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
November 11, 2019

How to Remove Whitespaces from a String in Java




In this blog, we will learn how to remove whitespaces from a string in Java. This will be asked in the most of the interviews. This can be implemented in three different ways listed below:

  1. Convert String into character array and use for loop
  2. For loop with charAt() method
  3. Using replaceAll() method

Method 1:

Convert String into Character array and use for loop

In the below snippet we have done the following

  • Created a string and assigned it with some value (includes whitespaces)
  • Created a character array and converted the string into the array by using .toCharArray() method
  • Created another empty string to store the output string
  • By using char.length() method we’ve iterated for loop
  • Used a If statement with a condition that to store only the values which are of not a period(whitespace) type.
public class RemoveWhitespacesString {

	public static void main(String[] args) {
		
		String str = "Java is a programming language";
		char ch [] = str.toCharArray();
		String str2 = "";
		for(int i=0; i<ch.length; i++)
		{
			if(ch[i]!=' ')
			{
				str2 = str2 + ch[i];
			}
		}
		
		System.out.println(str2);
	}
}
Output:
-------
Javaisaprogramminglanguage

Method 2:

Convert String into Character array and use for loop with CharAt() method

In the below snippet we have done the following

  • Created a string and assigned it with some value (includes whitespaces)
  • Created a character array and converted the string into the array by using .toCharArray() method
  • Created another empty string to store the output string
  • By using char.length() method we’ve iterated for loop
  • Used a If statement with a condition that to store only the values which are of not a period(whitespace) type.
  • Stored all the values into output string using charAt() method
public class RemoveWhitespacesString {

	public static void main(String[] args) {
		
		String str = "Java is a programming language";
		char ch [] = str.toCharArray();	
		String str3="";
		for(int i=0; i<ch.length; i++)
		{
			if(ch[i]!=' ')
			{
				str3 = str3 + str.charAt(i);
			}
			
		}
		System.out.println(str3);
		
	}
	
}
Output:
-------
Javaisaprogramminglanguage



Method 3:

Using replaceAll() method:

In the below snippet, we have done the following:

  • Created a string and assigned it with some value (includes whitespaces)
  • Created another string to store the output value
  • Using replaceAll() method with a regular expression which is a built-in method of string class we stored the value into the output string
public class RemoveWhitespacesString {

	public static void main(String[] args) {
		
		String str = "Java is a programming language";
		String str4=str.replaceAll(" ", "");
		System.out.println(str4);
	}
	
}

Output:
-------
Javaisaprogramminglanguage


Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook on Google+

How to Perform Sum of the Digits in a Given Number in Java How to Compare Two Arrays in Java using built-in functions

Related Posts

How To SWAP Two Numbers in Java Without using Temp Variable

Java IQs

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

How to Read Properties file in Java

Java IQs, Uncategorized

How to Read Data From Properties File in Java

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Compare two arrays in java

Java IQs

How to Compare Two Arrays in Java using built-in functions

Sum of The digits in a number in Java

Java IQs

How to Perform Sum of the Digits in a Given Number in Java

How To Reverse A String in Java

Java IQs

How to Reverse a String in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023