852

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"
Mateen Ulhaq
27.9k22 gold badges122 silver badges155 bronze badges
asked Mar 28, 2011 at 7:22
7
  • 1
    \\W means all non-words see download.oracle.com/javase/6/docs/api/java/util/regex/… Commented Mar 28, 2011 at 7:27
  • 92
    What's your plan with the "name=johnage=13year=2001" string? Not to parse it I hope. Commented Mar 28, 2011 at 7:45
  • 6
    @JonasElfström I imagine its to help with string comparisons Commented Jan 11, 2015 at 1:42
  • how about if the string is actually = " " . Is all what trims() does is clearing the empty string just like i mentioned? @zyamat ? Commented Nov 7, 2016 at 10:49
  • Possible duplicate of How to remove white space in java string Commented Apr 27, 2017 at 10:50

36 Answers 36

1
2
1533

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+","")
Dave Jarvis
31.3k43 gold badges186 silver badges327 bronze badges
answered Mar 28, 2011 at 7:24
Sign up to request clarification or add additional context in comments.

6 Comments

I would like to note that these two regex's will produce different results if you are looking to replace all whitespace with a single space (or some other set of characters). If you have consecutive spaces, using \\s it will replace each whitespace character with the provided characters given. Given \\s+ it will replace each set of whitespaces with a single replacement string. I can see quite a few case where people may be coming to this post for replacing whitespace with something that is not just an empty string, and this may be helpful.
but it doesn't remove the white-space if it is at the beginning of the string.
@lonesome use .trim() for that
Just use StringUtils from apache-commons. Its a static method called StringUtils.deleteWhitespace.
if you want to use this method in a loop, you should define Pattern as final variable Pattern.compile("\\s") because replaceAll recompile pattern on every call return Pattern.compile(regex).matcher(this).replaceAll(replacement);.
|
291
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.)

answered Mar 28, 2011 at 7:26

Comments

123

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.)

answered Apr 16, 2013 at 10:47

2 Comments

Does this mean we can overwrite the original string by writing for example: S = S.replaceAll("\\s", ""); whereas first the replacing will be done and then S will receive the characterstripped version of S
@frogeyedpeas That overwrites the variable S but it doesn't overwrite the string that S points to.
87

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.

answered Jul 20, 2014 at 17:34

1 Comment

Will not work for NBSP characters.
74

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

answered Mar 28, 2011 at 7:25

1 Comment

What a difference being a minute late can make!
59

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?

answered Jul 15, 2013 at 18:44

3 Comments

I typed out a quick example to check this because it sounded odd to me and found that the added plus sign isn't needed. Multiple spaces separating words are consumed. The reason for this is most likely that replaceAll repeats until the pattern doesn't match any part of the string.
Indeed. The + may make it marginally more CPU friendly, because consecutive whitespace is handled in a single replace operation, but that's the only difference in this case. It's indeed the All, not the + that's replacing non-consecutive whitespace in the string.
it does not delete this (u00A0)
53

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

st.replaceAll("[\\s|\\u00A0]+", "");
Amer Qarabsa
6,6343 gold badges26 silver badges51 bronze badges
answered Nov 14, 2014 at 14:13

3 Comments

This fails for: " ab c "
@MohdFarid Applied a fix, have to be ok now.
I think it should be st.replaceAll("[\\s|\u00A0]+", "");
35

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

answered Jan 25, 2013 at 11:51

Comments

26

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

answered Mar 28, 2011 at 7:24

Comments

12

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.

OneCricketeer
193k20 gold badges147 silver badges277 bronze badges
answered Oct 20, 2016 at 4:47

Comments

9

You can do it so simply by

String newMysz = mysz.replace(" ","");
ArtKorchagin
4,86913 gold badges45 silver badges58 bronze badges
answered Dec 28, 2015 at 10:42

2 Comments

Amazingly, the only worker in my situation. Thanks.
Whitespace is not always a space char. ;-)
6
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
answered Mar 30, 2014 at 13:24

Comments

6

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
 )
 );
 }
}
answered May 26, 2020 at 7:47

1 Comment

Performance doesn't matter. Code readability does.
4
mysz = mysz.replace(" ","");

First with space, second without space.

Then it is done.

hichris123
10.3k15 gold badges58 silver badges70 bronze badges
answered May 7, 2013 at 8:30

1 Comment

Just to clarify, whitespace means [ \t\n\x0B\f\r]. You are only doing normal [ ] spaces.
4
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

answered Feb 16, 2016 at 23:18

1 Comment

Very useful, but doesn't answer the posted question!
4

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

FelixSFD
6,10110 gold badges46 silver badges135 bronze badges
answered May 9, 2017 at 13:36

1 Comment

there is answer like/equals your suggestion, above
4

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

"myString".replace(Regex("\\s+"), "")
answered Jan 4, 2019 at 21:04

Comments

3

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

answered Mar 28, 2011 at 7:26

1 Comment

Where's the rest of this answer?
3

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.

answered Apr 2, 2013 at 13:11

1 Comment

This information has been provided by others years before you posted your answer.
3

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); 
 }
}
fredmaggiowski
2,2483 gold badges28 silver badges45 bronze badges
answered Jun 21, 2016 at 18:17

Comments

3

Use apache string util class is better to avoid NullPointerException

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

Output

abcdef
Gowthaman M
8,3029 gold badges40 silver badges57 bronze badges
answered Oct 7, 2017 at 6:15

Comments

3
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);
 
 
 }
}
answered Jan 10, 2021 at 6:42

1 Comment

How does this answer differ from the other already existing answers?
2
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);
 }
}
L Joey
1552 silver badges8 bronze badges
answered Nov 2, 2015 at 22:36

Comments

2

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.

answered Aug 1, 2017 at 7:00

1 Comment

But a very inefficient solution. And, as you can see from the other solutions - this only works for " " space - and not for different kinds of whitespaces.
1

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

answered Oct 14, 2015 at 16:25

Comments

1

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);
}
answered Jul 3, 2018 at 17:06

Comments

1

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]","")

answered Jul 18, 2018 at 10:57

Comments

0

use StrUtil.cleanBlank(CharSequence str) by hutool

<dependency>
 <groupId>cn.hutool</groupId>
 <artifactId>hutool-all</artifactId>
 <version>5.8.16</version>
</dependency>

answered Feb 4, 2024 at 10:06

Comments

-1

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); 
 }
}
Samuel Philipp
11.1k12 gold badges41 silver badges58 bronze badges
answered Jun 12, 2018 at 19:26

Comments

-1
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.

Samuel Philipp
11.1k12 gold badges41 silver badges58 bronze badges
answered Jun 5, 2018 at 9:22

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.