Java how to replace 2 or more spaces with single s

2018-12-31 21:48发布

问题:

Looking for quick, simple way in Java to change this string

\" hello     there   \"

to something that looks like this

\"hello there\"

where I replace all those multiple spaces with a single space, except I also want the one or more spaces at the beginning of string to be gone.

Something like this gets me partly there

String mytext = \" hello     there   \";
mytext = mytext.replaceAll(\"( )+\", \" \");

but not quite.

回答1:

Try this:

String after = before.trim().replaceAll(\" +\", \" \");

See also

  • String.trim()
    • Returns a copy of the string, with leading and trailing whitespace omitted.
  • regular-expressions.info/Repetition

No trim() regex

It\'s also possible to do this with just one replaceAll, but this is much less readable than the trim() solution. Nonetheless, it\'s provided here just to show what regex can do:

    String[] tests = {
        \"  x  \",          // [x]
        \"  1   2   3  \",  // [1 2 3]
        \"\",               // []
        \"   \",            // []
    };
    for (String test : tests) {
        System.out.format(\"[%s]%n\",
            test.replaceAll(\"^ +| +$|( )+\", \"$1\")
        );
    }

There are 3 alternates:

  • ^_+ : any sequence of spaces at the beginning of the string
    • Match and replace with $1, which captures the empty string
  • _+$ : any sequence of spaces at the end of the string
    • Match and replace with $1, which captures the empty string
  • (_)+ : any sequence of spaces that matches none of the above, meaning it\'s in the middle
    • Match and replace with $1, which captures a single space

See also

  • regular-expressions.info/Anchors


回答2:

You just need a:

replaceAll(\"\\\\s{2,}\", \" \").trim();

where you match one or more spaces and replace them with a single space and then trim whitespaces at the beginning and end (you could actually invert by first trimming and then matching to make the regex quicker as someone pointed out).

To test this out quickly try:

System.out.println(new String(\" hello     there   \").trim().replaceAll(\"\\\\s{2,}\", \" \"));

and it will return:

\"hello there\"


回答3:

Use the Apache commons StringUtils.normalizeSpace(String str) method. See docs here



回答4:

This worked perfectly for me : sValue = sValue.trim().replaceAll(\"\\\\s+\", \" \");



回答5:

To eliminate spaces at the beginning and at the end of the String, use String#trim() method. And then use your mytext.replaceAll(\"( )+\", \" \").



回答6:

You can first use String.trim(), and then apply the regex replace command on the result.



回答7:

Try this one.

Sample Code

String str = \" hello     there   \";
System.out.println(str.replaceAll(\"( +)\",\" \").trim());

OUTPUT

hello there

First it will replace all the spaces with single space. Than we have to supposed to do trim String because Starting of the String and End of the String it will replace the all space with single space if String has spaces at Starting of the String and End of the String So we need to trim them. Than you get your desired String.



回答8:

\"[ ]{2,}\"

This will match more than one space.

String mytext = \" hello     there   \";
//without trim -> \" hello there\"
//with trim -> \"hello there\"
mytext = mytext.trim().replaceAll(\"[ ]{2,}\", \" \");
System.out.println(mytext);

OUTPUT:

hello there


回答9:

You could use lookarounds also.

test.replaceAll(\"^ +| +$|(?<= ) \", \"\");

OR

test.replaceAll(\"^ +| +$| (?= )\", \"\")

<space>(?= ) matches a space character which is followed by another space character. So in consecutive spaces, it would match all the spaces except the last because it isn\'t followed by a space character. This leaving you a single space for consecutive spaces after the removal operation.

Example:

    String[] tests = {
            \"  x  \",          // [x]
            \"  1   2   3  \",  // [1 2 3]
            \"\",               // []
            \"   \",            // []
        };
        for (String test : tests) {
            System.out.format(\"[%s]%n\",
                test.replaceAll(\"^ +| +$| (?= )\", \"\")
            );
        }


回答10:

String str = \" hello world\"

reduce spaces first

str = str.trim().replaceAll(\" +\", \" \");

capitalize the first letter and lowercase everything else

str = str.substring(0,1).toUpperCase() +str.substring(1,str.length()).toLowerCase();


回答11:

This worked for me

scan= filter(scan, \" [\\\\s]+\", \" \");
scan= sac.trim();

where filter is following function and scan is the input string:

public String filter(String scan, String regex, String replace) {
    StringBuffer sb = new StringBuffer();

    Pattern pt = Pattern.compile(regex);
    Matcher m = pt.matcher(scan);

    while (m.find()) {
        m.appendReplacement(sb, replace);
    }

    m.appendTail(sb);

    return sb.toString();
}


回答12:

trim()

Removes only the leading & trailing spaces.

From Java Doc, \"Returns a string whose value is this string, with any leading and trailing whitespace removed.\"

System.out.println(\" D ev  Dum my \".trim());

\"D ev Dum my\"

replace(), replaceAll()

Replaces all the empty strings in the word,

System.out.println(\" D ev  Dum my \".replace(\" \",\"\"));

System.out.println(\" D ev  Dum my \".replaceAll(\" \",\"\"));

System.out.println(\" D ev  Dum my \".replaceAll(\"\\\\s+\",\"\"));

Output:

\"DevDummy\"

\"DevDummy\"

\"DevDummy\"

Note: \"\\s+\" is the regular expression similar to the empty space character.

Reference : https://www.codedjava.com/2018/06/replace-all-spaces-in-string-trim.html



回答13:

See String.replaceAll.

Use the regex \"\\s\" and replace with \" \".

Then use String.trim.



回答14:

check this...

public static void main(String[] args) {
    String s = \"A B  C   D    E F      G\\tH I\\rJ\\nK\\tL\";
    System.out.println(\"Current      : \"+s);
    System.out.println(\"Single Space : \"+singleSpace(s));
    System.out.println(\"Space  count : \"+spaceCount(s));
    System.out.format(\"Replace  all = %s\", s.replaceAll(\"\\\\s+\", \"\"));

    // Example where it uses the most.
    String s = \"My name is yashwanth . M\";
    String s2 = \"My nameis yashwanth.M\";

    System.out.println(\"Normal  : \"+s.equals(s2));
    System.out.println(\"Replace : \"+s.replaceAll(\"\\\\s+\", \"\").equals(s2.replaceAll(\"\\\\s+\", \"\")));

} 

If String contains only single-space then replace() will not-replace,

If spaces are more than one, Then replace() action performs and removes spacess.

public static String singleSpace(String str){
    return str.replaceAll(\"  +|   +|\\t|\\r|\\n\",\"\");
}

To count the number of spaces in a String.

public static String spaceCount(String str){
    int i = 0;
    while(str.indexOf(\" \") > -1){
      //str = str.replaceFirst(\" \", \"\"+(i++));
        str = str.replaceFirst(Pattern.quote(\" \"), \"\"+(i++)); 
    }
    return str;
}

Pattern.quote(\"?\") returns literal pattern String.



回答15:

My method before I found the second answer using regex as a better solution. Maybe someone needs this code.

private String replaceMultipleSpacesFromString(String s){
    if(s.length() == 0 ) return \"\";

    int timesSpace = 0;
    String res = \"\";

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);

        if(c == \' \'){
            timesSpace++;
            if(timesSpace < 2)
                res += c;
        }else{
            res += c;
            timesSpace = 0;
        }
    }

    return res.trim();
}


回答16:

you should do it like this

String mytext = \" hello     there   \";
mytext = mytext.replaceAll(\"( +)\", \" \");

put + inside round brackets.



回答17:

Stream version, filters spaces and tabs.

Stream.of(str.split(\"[ \\\\t]\")).filter(s -> s.length() > 0).collect(Collectors.joining(\" \"))


回答18:

public class RemoveExtraSpacesEfficient {

    public static void main(String[] args) {

        String s = \"my    name is    mr    space \";

        char[] charArray = s.toCharArray();

        char prev = s.charAt(0);

        for (int i = 0; i < charArray.length; i++) {
            char cur = charArray[i];
            if (cur == \' \' && prev == \' \') {

            } else {
                System.out.print(cur);
            }
            prev = cur;
        }
    }
}

The above solution is the algorithm with the complexity of O(n) without using any java function.



回答19:

Please use below code

package com.myjava.string;

import java.util.StringTokenizer;

public class MyStrRemoveMultSpaces {

    public static void main(String a[]){

        String str = \"String    With Multiple      Spaces\";

        StringTokenizer st = new StringTokenizer(str, \" \");

        StringBuffer sb = new StringBuffer();

        while(st.hasMoreElements()){
            sb.append(st.nextElement()).append(\" \");
        }

        System.out.println(sb.toString().trim());
    }
}