我有一个这样的字符串:

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

我想删除字符串中的空格。我尝试了trim(),但这只删除了整个字符串前后的空格。我还尝试了replaceAll(“\\W”,“”),但随后=也被删除。

如何通过以下方式实现字符串:

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

当前回答

最简单的方法是使用commons-lang3库的org.apachemons.lang3.StringUtils类,例如“commons-lang3-3.1.jar”。

对输入字符串使用静态方法“StringUtils.deleteWhitespace(Stringstr)”&它将在删除所有空格后返回一个字符串。我尝试了示例字符串“name=johnage=13year=2001”&它返回了您想要的字符串-“name=johnage=13yeal=2001”。希望这有帮助。

其他回答

处理字符串操作的一种方法是来自Apache commons的StringUtils。

String withoutWhitespace = StringUtils.deleteWhitespace(whitespaces);

你可以在这里找到它。commons-lang包含更多内容,并且得到了很好的支持。

在java中,我们可以执行以下操作:

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

为此,您需要将以下程序包导入到程序中:

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

我希望这对你有帮助。

提供了相当多的答案。我想给出一个比正则表达式更易读、更好的解决方案。

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();

    }

}

您还可以查看下面的Java代码。以下代码不使用任何“内置”方法。

/**
 * 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));

    }
}

输入:

String inp = "01239Debashish123Pattn456aik";

输出:

The String is: 01239123456

使用mysz.replaceAll(“\\s+”,“”);