简体   繁体   English

无法替换字符串java中的空格

[英]unable to replace blank space in String java

bit of a weird one this as I have checked it over multiple times and still cannot see anything wrong with what I am doing.这有点奇怪,因为我已经多次检查过它,但仍然看不出我在做什么有什么问题。

Anyway, I have this string named score that is a method parameter.无论如何,我有一个名为 score 的字符串,它是一个方法参数。 The input is "0 - 2" , I would like to parse this string but before I must remove the blank spaces.输入是"0 - 2" ,我想解析这个字符串,但在我必须删除空格之前。 I've tried using score = score.replaceAll("\\\\s+", "") and score = score.replace(" ", "") however the blank spaces are still not replaced.我试过使用score = score.replaceAll("\\\\s+", "")score = score.replace(" ", "")但是空格仍然没有被替换。 Underneath this line of code I print out the score and it is still displaying the string as "0 - 2" with the spaces not deleted.在这行代码下面,我打印出分数,它仍然将字符串显示为"0 - 2" ,但没有删除空格。 Any help would be much appreciated.任何帮助将非常感激。

Here is the code for the method这是该方法的代码

public static int parseScore(String score, boolean first){

    score = score.replaceAll("\\s+",""); // remove white space
    System.out.println(score);

    String[] tokens = score.split("-");

    if(first == true){

        return Integer.parseInt(tokens[0]);

    }else{

        return Integer.parseInt(tokens[1]);

    }

}

Error:错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0 " at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at PredictResult2.parseScore(PredictResult2.java:32) at PredictResult2.main(PredictResult2.java:102)线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“0” at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer。 parseInt(Unknown Source) at PredictResult2.parseScore(PredictResult2.java:32) at PredictResult2.main(PredictResult2.java:102)

EDIT:编辑:

Forgot to mention that the string that I am using has already been parsed from a downloaded html page.忘了提到我正在使用的字符串已经从下载的 html 页面中解析出来了。 Could that have anything to do with it?会不会跟这有关系? maybe different character formatting or something?也许不同的字符格式或什么?

Your code is perfectly fine.你的代码非常好。 It works just fine when I tested it, so that's not it.当我测试它时它工作得很好,所以不是这样。 It must be some other odd problem.这一定是其他一些奇怪的问题。

But anyways, for what you're trying to achieve maybe trim() would be more efficient and quicker.但无论如何,对于您想要实现的目标, trim()可能会更高效、更快。

    public static int parseScore(String score, boolean first){
    String[] tokens = score.split("-");
    if(first == true){
        return Integer.parseInt(tokens[0].trim());
    }else{
        return Integer.parseInt(tokens[1].trim());

    }
}

Seems to be a case of dealing with non-breaking spaces.似乎是处理不间断空格的情况。 Stumbled about these a while ago and wrote myself the following util method.不久前偶然发现了这些,并为自己编写了以下 util 方法。 More information is found here:在这里可以找到更多信息:

Why is non-breaking space not a whitespace character in java? 为什么不间断空格不是java中的空白字符?

https://en.wikipedia.org/wiki/Non-breaking_space https://en.wikipedia.org/wiki/Non-breaking_space

/**
 * Trims non-breaking whitespace, too.
 */
public static String trim(String value) {
    return value.replaceAll("[\\s\\u00A0]+$", "");
}

Your input must be wrong.你的输入肯定是错误的。 Your code works good - check this piece of code:您的代码运行良好 - 检查这段代码:

public static void parseScore(){

    String score = "0 - 2";
    score = score.replaceAll("\\s+","");

    System.out.println(score);

    String[] tokens = score.split("-");

    System.out.println(tokens[0]);
    System.out.println(tokens[1]);


    System.out.println(Integer.parseInt(tokens[0]));
    System.out.println(Integer.parseInt(tokens[1]));
}

public static void main(String[] args) {
    parseScore();
}

If you are not sure about the input I suggest converting the string to char array and printing the ASCII code of each character.如果您不确定输入,我建议将字符串转换为字符数组并打印每个字符的 ASCII 代码。 Then compare the output with the table here .然后将输出与此处的表格进行比较。

char[] array = score.toCharArray();
    for (char c : array)
        System.out.print((int)c + " ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM