Removing whitespace from strings in Java

JavaWhitespace

Java Problem Overview


I have a string like this:

mysz = "name=john age=13 year=2001";

I want to remove the whitespaces in the string. I tried trim() but this removes only whitespaces before and after the whole string. I also tried replaceAll("\\W", "") but then the = also gets removed.

How can I achieve a string with:

mysz2 = "name=johnage=13year=2001"

Java Solutions


Solution 1 - Java

st.replaceAll("\\s+","") removes all whitespaces and non-visible characters (e.g., tab, \n).


st.replaceAll("\\s+","") and st.replaceAll("\\s","") produce the same result.

The second regex is 20% faster than the first one, but as the number consecutive spaces increases, the first one performs better than the second one.


Assign the value to a variable, if not used directly:

st = st.replaceAll("\\s+","")

Solution 2 - Java

replaceAll("\\s","")

\w = Anything that is a word character

\W = Anything that isn't a word character (including punctuation etc)

\s = Anything that is a space character (including space, tab characters etc)

\S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)

(Edit: As pointed out, you need to escape the backslash if you want \s to reach the regex engine, resulting in \\s.)

Solution 3 - Java

The most correct answer to the question is:

String mysz2 = mysz.replaceAll("\\s","");

I just adapted this code from the other answers. I'm posting it because besides being exactly what the question requested, it also demonstrates that the result is returned as a new string, the original string is not modified as some of the answers sort of imply.

(Experienced Java developers might say "of course, you can't actually modify a String", but the target audience for this question may well not know this.)

Solution 4 - Java

How about replaceAll("\\s", ""). Refer here.

Solution 5 - Java

One way to handle String manipulations is StringUtils from Apache commons.

String withoutWhitespace = StringUtils.deleteWhitespace(whitespaces);

You can find it here. commons-lang includes lots more and is well supported.

Solution 6 - Java

If you need to remove unbreakable spaces too, you can upgrade your code like this :

st.replaceAll("[\\s|\\u00A0]+", "");

Solution 7 - Java

You should use

s.replaceAll("\\s+", "");

instead of:

s.replaceAll("\\s", "");

This way, it will work with more than one spaces between each string. The + sign in the above regex means "one or more \s"

--\s = Anything that is a space character (including space, tab characters etc). Why do we need s+ here?

Solution 8 - Java

If you prefer utility classes to regexes, there is a method trimAllWhitespace(String) in StringUtils in the Spring Framework.

Solution 9 - Java

You've already got the correct answer from Gursel Koca but I believe that there's a good chance that this is not what you really want to do. How about parsing the key-values instead?

import java.util.Enumeration;
import java.util.Hashtable;
 
class SplitIt {
  public static void main(String args[])  {
    
    String person = "name=john age=13 year=2001";

    for (String p : person.split("\\s")) {
      String[] keyValue = p.split("=");
      System.out.println(keyValue[0] + " = " + keyValue[1]);
    }
  }
}

> output:
> name = john
> age = 13
> year = 2001

Solution 10 - Java

The easiest way to do this is by using the org.apache.commons.lang3.StringUtils class of commons-lang3 library such as "commons-lang3-3.1.jar" for example.

Use the static method "StringUtils.deleteWhitespace(String str)" on your input string & it will return you a string after removing all the white spaces from it. I tried your example string "name=john age=13 year=2001" & it returned me exactly the string that you wanted - "name=johnage=13year=2001". Hope this helps.

Solution 11 - Java

You can do it so simply by

String newMysz = mysz.replace(" ","");

Solution 12 - Java

public static void main(String[] args) {		
	String s = "name=john age=13 year=2001";
	String t = s.replaceAll(" ", "");
	System.out.println("s: " + s + ", t: " + t);
}

Output:
s: name=john age=13 year=2001, t: name=johnage=13year=2001

Solution 13 - Java

I am trying an aggregation answer where I test all ways of removing all whitespaces in a string. Each method is ran 1 million times and then then the average is taken. Note: Some compute will be used on summing up all the runs.


##Results:

1st place from @jahir 's answer

  • StringUtils with short text: 1.21E-4 ms (121.0 ms)
  • StringUtils with long text: 0.001648 ms (1648.0 ms)

2nd place

  • String builder with short text: 2.48E-4 ms (248.0 ms)
  • String builder with long text: 0.00566 ms (5660.0 ms)

3rd place

  • Regex with short text: 8.36E-4 ms (836.0 ms)
  • Regex with long text: 0.008877 ms (8877.0 ms)

4th place

  • For loop with short text: 0.001666 ms (1666.0 ms)
  • For loop with long text: 0.086437 ms (86437.0 ms)

##Here is the code:

public class RemoveAllWhitespaces {
    public static String Regex(String text){
        return text.replaceAll("\\s+", "");
    }

    public static String ForLoop(String text) {
        for (int i = text.length() - 1; i >= 0; i--) {
            if(Character.isWhitespace(text.codePointAt(i))) {
                text = text.substring(0, i) + text.substring(i + 1);
            }
        }

        return text;
    }

    public static String StringBuilder(String text){
        StringBuilder builder = new StringBuilder(text);
        for (int i = text.length() - 1; i >= 0; i--) {
            if(Character.isWhitespace(text.codePointAt(i))) {
                builder.deleteCharAt(i);
            }
        }

        return builder.toString();
    }
}

##Here are the tests:

import org.junit.jupiter.api.Test;

import java.util.function.Function;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

public class RemoveAllWhitespacesTest {
    private static final String longText = "123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222123 123 \t 1adc \n 222";
    private static final String expected = "1231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc2221231231adc222";

    private static final String shortText = "123 123 \t 1adc \n 222";
    private static final String expectedShortText = "1231231adc222";

    private static final int numberOfIterations = 1000000;

    @Test
    public void Regex_LongText(){
        RunTest("Regex_LongText", text -> RemoveAllWhitespaces.Regex(text), longText, expected);
    }

    @Test
    public void Regex_ShortText(){
        RunTest("Regex_LongText", text -> RemoveAllWhitespaces.Regex(text), shortText, expectedShortText);

    }

    @Test
    public void For_LongText(){
        RunTest("For_LongText", text -> RemoveAllWhitespaces.ForLoop(text), longText, expected);
    }

    @Test
    public void For_ShortText(){
        RunTest("For_LongText", text -> RemoveAllWhitespaces.ForLoop(text), shortText, expectedShortText);
    }

    @Test
    public void StringBuilder_LongText(){
        RunTest("StringBuilder_LongText", text -> RemoveAllWhitespaces.StringBuilder(text), longText, expected);
    }

    @Test
    public void StringBuilder_ShortText(){
        RunTest("StringBuilder_ShortText", text -> RemoveAllWhitespaces.StringBuilder(text), shortText, expectedShortText);
    }

    private void RunTest(String testName, Function<String,String> func, String input, String expected){
        long startTime = System.currentTimeMillis();
        IntStream.range(0, numberOfIterations)
                .forEach(x -> assertEquals(expected, func.apply(input)));
        double totalMilliseconds = (double)System.currentTimeMillis() - (double)startTime;
        System.out.println(
                String.format(
                        "%s: %s ms (%s ms)",
                        testName,
                        totalMilliseconds / (double)numberOfIterations,
                        totalMilliseconds
                )
        );
    }
}

Solution 14 - Java

mysz = mysz.replace(" ","");

First with space, second without space.

Then it is done.

Solution 15 - Java

String a="string with                multi spaces ";
//or this 
String b= a.replaceAll("\\s+"," ");
String c= a.replace("    "," ").replace("   "," ").replace("  "," ").replace("   "," ").replace("  "," ");

//it work fine with any spaces *don't forget space in sting b

Solution 16 - Java

Use mysz.replaceAll("\\s+","");

Solution 17 - Java

Use apache string util class is better to avoid NullPointerException

org.apache.commons.lang3.StringUtils.replace("abc def ", " ", "")

Output

abcdef

Solution 18 - Java

\W means "non word character". The pattern for whitespace characters is \s. This is well documented in the Pattern javadoc.

Solution 19 - Java

In java we can do following operation:

String pattern="[\\s]";
String replace="";
part="name=john age=13 year=2001";
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(part);
part=m.replaceAll(replace);
System.out.println(part);

for this you need to import following packages to your program:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

i hope it will help you.

Solution 20 - Java

Using Pattern And Matcher it is more Dynamic.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RemovingSpace {

	/**
     * @param args
	 * Removing Space Using Matcher
     */
	public static void main(String[] args) {
	    String str= "jld fdkjg jfdg ";
		String pattern="[\\s]";
    	String replace="";

	    Pattern p= Pattern.compile(pattern);
		Matcher m=p.matcher(str);

    	str=m.replaceAll(replace);
	    System.out.println(str);	
    }
}

Solution 21 - Java

When using st.replaceAll("\\s+","") in Kotlin, make sure you wrap "\\s+" with Regex:

"myString".replace(Regex("\\s+"), "")

Solution 22 - Java

import java.util.*;
public class RemoveSpace {
    public static void main(String[] args) {
        String mysz = "name=john age=13 year=2001";
        Scanner scan = new Scanner(mysz);

        String result = "";
        while(scan.hasNext()) {
            result += scan.next();
        }
        System.out.println(result);
    }
}

Solution 23 - Java

To remove spaces in your example, this is another way to do it:

String mysz = "name=john age=13 year=2001";
String[] test = mysz.split(" ");
mysz = String.join("", mysz);

What this does is it converts it into an array with the spaces being the separators, and then it combines the items in the array together without the spaces.

It works pretty well and is easy to understand.

Solution 24 - Java

there are many ways to solve this problem. you can use split function or replace function of Strings.

for more info refer smilliar problem http://techno-terminal.blogspot.in/2015/10/how-to-remove-spaces-from-given-string.html

Solution 25 - Java

There are others space char too exists in strings.. So space char we may need to replace from strings.

Ex: NO-BREAK SPACE, THREE-PER-EM SPACE, PUNCTUATION SPACE

Here is the list of space char http://jkorpela.fi/chars/spaces.html

So we need to modify

\u2004 us for THREE-PER-EM SPACE

s.replaceAll("[\u0020\u2004]","")

Solution 26 - Java

public static String removeWhiteSpaces(String str){
	String s = "";
	char[] arr = str.toCharArray();
	for (int i = 0; i < arr.length; i++) {
		int temp = arr[i];
		if(temp != 32 && temp != 9) { // 32 ASCII for space and 9 is for Tab
			s += arr[i];
		}
	}
	return s;
}

This might help.

Solution 27 - Java

Separate each group of text into its own substring and then concatenate those substrings:

public Address(String street, String city, String state, String zip ) {
	this.street = street;
	this.city = city;
	// Now checking to make sure that state has no spaces...
	int position = state.indexOf(" ");
	if(position >=0) {
		//now putting state back together if it has spaces...
		state = state.substring(0, position) + state.substring(position + 1);  
	}
}

Solution 28 - Java

White space can remove using isWhitespace function from Character Class.

public static void main(String[] args) {
    String withSpace = "Remove white space from line";
    StringBuilder removeSpace = new StringBuilder();
    
    for (int i = 0; i<withSpace.length();i++){
        if(!Character.isWhitespace(withSpace.charAt(i))){
            removeSpace=removeSpace.append(withSpace.charAt(i));
        }
    }
    System.out.println(removeSpace);
}

Solution 29 - Java

You can also take a look at the below Java code. Following codes does not use any "built-in" methods.

/**
 * Remove all characters from an alphanumeric string.
 */
public class RemoveCharFromAlphanumerics {

	public static void main(String[] args) {

		String inp = "01239Debashish123Pattn456aik";

		char[] out = inp.toCharArray();
		
		int totint=0;

		for (int i = 0; i < out.length; i++) {
			System.out.println(out[i] + " : " + (int) out[i]);
			if ((int) out[i] >= 65 && (int) out[i] <= 122) {
				out[i] = ' ';
			}
			else {
				totint+=1;
			}

		}

		System.out.println(String.valueOf(out));
		System.out.println(String.valueOf("Length: "+ out.length));
		
		for (int c=0; c<out.length; c++){
			
			System.out.println(out[c] + " : " + (int) out[c]);
			
			if ( (int) out[c] == 32) {
				System.out.println("Its Blank");
				 out[c] = '\'';
			}
		
		}
	
		System.out.println(String.valueOf(out));
		
		System.out.println("**********");
		System.out.println("**********");
		char[] whitespace = new char[totint];
		int t=0;
		for (int d=0; d< out.length; d++) {
			
			int fst =32;
			
			
						
			if ((int) out[d] >= 48 && (int) out[d] <=57 ) {
				
				System.out.println(out[d]);
				whitespace[t]= out[d];
				t+=1;
			
			}
			
		}
		
		System.out.println("**********");
		System.out.println("**********");
		
		System.out.println("The String is: " + String.valueOf(whitespace));
	
	}
}

Input:

String inp = "01239Debashish123Pattn456aik";

Output:

The String is: 01239123456

Solution 30 - Java

private String generateAttachName(String fileName, String searchOn, String char1) {
	return fileName.replaceAll(searchOn, char1);
}


String fileName= generateAttachName("Hello My Mom","\\s","");

Solution 31 - Java

Quite a lot of answers are provided. I would like to give a solution which is quite readable and better than regex.

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

public class RemoveAllWhitespaceTest {

	public static void main(String[] args) throws IOException {

		String str1 = "\n\tThis is my string \n \r\n  !";
		
		System.out.println("[" + str1 + "]");
		
        System.out.println("Whitespace Removed:");

		System.out.println("[" + StringUtils.deleteWhitespace(str1) + "]");

		System.out.println();

	}

}

Solution 32 - Java

package com.sanjayacchana.challangingprograms;

public class RemoveAllWhiteSpacesInString {

	public static void main(String[] args) {
		
		String str = "name=john age=13 year=2001";
		
		str = str.replaceAll("\\s", ""); 
		
		System.out.println(str);
		
		
	}

}

Solution 33 - Java

public String removeSpaces(String word){
		StringBuffer buffer = new StringBuffer();
		if(word != null){
			char[] arr = word.toCharArray();
			for(int i=0; i < arr.length; i++){
				if(!Character.isSpaceChar(arr[i])) {
					buffer.append(arr[i]);
				}   			   
			}
		}
		return buffer.toString();
	}

Solution 34 - Java

You can achieve this without using replaceAll() or any Predefined Method in Java. this way is preferred:-

public class RemoveSpacesFromString {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String newString;
		String str = "prashant is good" ;
		int i;
		char[] strArray = str.toCharArray();
		StringBuffer sb =  new StringBuffer();
		
		for(i = 0; i < strArray.length; i++)
		{
			if(strArray[i] != ' ' && strArray[i] != '\t')
			{
				sb.append(strArray[i]);
			}
		}
		System.out.println(sb);
	}
}

Solution 35 - Java

The code you want is

str.replaceAll("\\s","");

This will remove all the white spaces.

Solution 36 - Java

If you want remove all white space from the string:

public String removeSpace(String str) {
	String result = "";
	for (int i = 0; i < str.length(); i++){
	    char c = str.charAt(i);        
	    if(c!=' ') {
	    	result += c;
	    }
	}
	return result;
}

Solution 37 - Java

Try this:

String str="name=john age=13 year=2001";
String s[]=str.split(" ");
StringBuilder v=new StringBuilder();
for (String string : s) {
	v.append(string);
}
str=v.toString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzyamatView Question on Stackoverflow
Solution 1 - JavaGursel KocaView Answer on Stackoverflow
Solution 2 - Javanitro2k01View Answer on Stackoverflow
Solution 3 - JavaFletchView Answer on Stackoverflow
Solution 4 - JavaErkan HaspulatView Answer on Stackoverflow
Solution 5 - JavajahirView Answer on Stackoverflow
Solution 6 - Javav.nivuahcView Answer on Stackoverflow
Solution 7 - JavaVarejonesView Answer on Stackoverflow
Solution 8 - JavakamczakView Answer on Stackoverflow
Solution 9 - JavaJonas ElfströmView Answer on Stackoverflow
Solution 10 - JavaAyaskantView Answer on Stackoverflow
Solution 11 - JavaVinod RangaView Answer on Stackoverflow
Solution 12 - JavaavngrView Answer on Stackoverflow
Solution 13 - JavaStian StandahlView Answer on Stackoverflow
Solution 14 - Javauser2357526View Answer on Stackoverflow
Solution 15 - JavafatsoftView Answer on Stackoverflow
Solution 16 - JavaMohammad RazaView Answer on Stackoverflow
Solution 17 - Javasendon1982View Answer on Stackoverflow
Solution 18 - JavaJB NizetView Answer on Stackoverflow
Solution 19 - Javauser27View Answer on Stackoverflow
Solution 20 - JavajayeshView Answer on Stackoverflow
Solution 21 - JavaJemshit IskenderovView Answer on Stackoverflow
Solution 22 - JavaTony NguyenView Answer on Stackoverflow
Solution 23 - JavaMegawattView Answer on Stackoverflow
Solution 24 - JavasatishView Answer on Stackoverflow
Solution 25 - JavaRakesh ChaudhariView Answer on Stackoverflow
Solution 26 - JavaRajesh GurbaniView Answer on Stackoverflow
Solution 27 - Javauser9832813View Answer on Stackoverflow
Solution 28 - JavaAbdur RahmanView Answer on Stackoverflow
Solution 29 - JavaDebView Answer on Stackoverflow
Solution 30 - JavaAbd AbughazalehView Answer on Stackoverflow
Solution 31 - JavaKunal VohraView Answer on Stackoverflow
Solution 32 - JavaSiva Sanjay AcchanaView Answer on Stackoverflow
Solution 33 - Javaharun ugurView Answer on Stackoverflow
Solution 34 - JavaPranshu SinghView Answer on Stackoverflow
Solution 35 - JavaHANUView Answer on Stackoverflow
Solution 36 - Javaedilon JuniorView Answer on Stackoverflow
Solution 37 - JavaDEVNAG BHARADView Answer on Stackoverflow