Java .charAt(i) 比较问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4173918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:20:14  来源:igfitidea点击:

Java .charAt(i) comparison issue

javastringcomparisonchar

提问by Carlos

Why when comparing a char against another it must be taken also from a string? For example;

为什么在将一个字符与另一个字符进行比较时,它也必须从字符串中获取?例如;

This does not work

这不起作用

   while(i < t.length() && zeroCount < 5) {
        if(t.charAt(i) == 0){
            zeroCount++;
        }
        i++;
    }

Nor does this

这也不行

char zero = 0;

      while(i < t.length() && zeroCount < 5) {
            if(t.charAt(i) == zero){
                zeroCount++;
            }
            i++;
        }

The only way I managed to get it working is like this...

我设法让它工作的唯一方法是这样的......

String zeros = "0000000000";

      while(i < t.length() && zeroCount < 5) {
            if(t.charAt(i) == zeros.charAt(i)){
                zeroCount++;
            }
            i++;
        }

Can anyone explain if am doing something wrong, or if it is just not acceptable to do it like the top 2 examples. If so, why?

任何人都可以解释我是否做错了什么,或者像前两个例子那样做是不可接受的。如果是这样,为什么?

采纳答案by skaffman

You're confusing

你很困惑

char zero = 0;

with

char zero = '0';

The former is the null-character (ASCII value of zero), whereas the latter is the character representing the digit zero.

前者是空字符(ASCII 值为零),而后者是表示数字零的字符。

This confusion is a rather unfortunate hang-over from C, with charvariables being treated as numbers as well as characters.

这种混淆是 C 的一个相当不幸的后遗症,char变量被视为数字和字符。

回答by Sean Owen

You are looking for the character '0'? Then compare to '0', not 0.

您正在寻找字符“0”?然后比较'0',而不是0。

回答by Jon Skeet

You're comparing against Unicode value 0 (aka U+0000, the "null" character) - which is not the same as the Unicode character representing the digit 0.

您正在与 Unicode 值 0(又名 U+0000,“空”字符)进行比较 - 这与表示数字 0的 Unicode 字符不同。

Use '0' instead of 0:

使用“0”代替 0:

while(i < t.length() && zeroCount < 5) {
    if(t.charAt(i) == '0'){
        zeroCount++;
    }
    i++;
}

回答by Stan Kurilin

Use '0' instead of 0.

使用“0”而不是 0。

回答by Hyman

The simple answer is that the value 0is not the same as the character '0'which has an ASCII code of 48(IIRC).

简单的答案是该值0'0'具有48(IIRC) ASCII 代码的字符不同。

You should compare it with the char value charAt(i) == '0'or subtract the char before comparison charAt(i) - '0' == 0

您应该将它与 char 值charAt(i) == '0'进行比较或在比较之前减去 charcharAt(i) - '0' == 0

回答by tchrist

These other answers have it right, but there's one very important thing you should know. You should never use chatAt! You should only use codePointAt.

这些其他答案是正确的,但是您应该知道一件非常重要的事情。你永远不应该使用chatAt!您应该只使用codePointAt.

Similarly, you mustn't blindly use i++to bump through a string. You need to see whether s.codePointAt(i) > Character.MAX_VALUEto know whether to give an extra i++kicker.

同样,您也不能盲目地使用i++撞线。你需要看看是否s.codePointAt(i) > Character.MAX_VALUE知道是否给一个额外的i++踢球者。

For example, to print out all the codepoints in a String sin standard "U+" notation:

例如,要String s以标准的“U+”表示法打印出 a中的所有代码点:

private static void say_U_contents(String s) { 
    System.out.print("U+");
    for (int i = 0; i < s.length(); i++) {
        System.out.printf("%X", s.codePointAt(i));
        if (s.codePointAt(i) > Character.MAX_VALUE) { i++; }  // UG!
        if (i+1 < s.length()) { System.out.printf("."); }
    }
}

That way you can output like U+61.DF, U+3C3, and U+1F4A9.1F4A9for the corresponding strings. That last one looks like "\uD83D\uDCA9\uD83D\uDCA9", which is simply insane.

这样你就可以为相应的字符串输出 like U+61.DF, U+3C3, and U+1F4A9.1F4A9。最后一个看起来像"\uD83D\uDCA9\uD83D\uDCA9",简直是疯了。