Remove extra spaces from string in java

0 votes

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"
Apr 13, 2018 in Java by developer_1
• 3,310 points

4 answers to this question.

0 votes

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 use directly:

st = st.replaceAll("\\s+","")
answered Apr 13, 2018 by Rishabh
• 3,620 points
0 votes

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

answered Jul 30, 2018 by Daisy
• 8,110 points
0 votes

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

st.replaceAll("[\\s|\\u00A0]+", "");
answered Sep 3, 2018 by Sushmita
• 6,890 points
0 votes
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String pattern="[\\s]";
String replace="";
part="name=john age=13 year=2001";
Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(part);
answered Sep 10, 2018 by Parth
• 4,630 points

Related Questions In Java

0 votes
1 answer

What do we understand from string pool in java?

This prints true (even though we don't use equals method: correct ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,110 points
0 votes
2 answers

Remove duplicate elements from an ArrayList in Java

List<String> al = new ArrayList<>(); // add elements ...READ MORE

answered Jul 26, 2018 in Java by Sushmita
• 6,890 points
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
0 votes
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,270 points
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 58,990 points
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
0 votes
1 answer

Formatting Double Value in Java

An alternative is to use String.format: double[] arr = ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,110 points
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,200 points
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,890 points