且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较字符串中的一个字符与另一个字符串

更新时间:2022-11-11 10:38:23

将字符初始化为由 charAt()

char current = hex.charAt(i);

然后在您的条件中使用字符型字符:

Then in your conditional use the literal char:

else if (current == 'b') 

由于 char 是一个原语,您可以使用 == 运算符来比较它。在前面的代码中,您使用 == 来比较 String ,因为 String 是一个 Object 代码检查它们是否相同 Object 如果它们有与 String.equals()方法的值相同。

Since char is a primitive you can compare it using the == operator. In the former code you were comparing a String using ==, since a String is an Object the code is checking to see if they are the same Object not if they have the same value as the String.equals() method would.