gpt4 book ai didi

java - 为什么 indexOf 没有给出来自 unicode 的正确结果?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:44:49 26 4
gpt4 key购买 nike

我不明白为什么如果我将 indexOf 与 char 的 Unicode 表示一起使用,我得不到正确的结果?

public class Test {
public static void main(String[] args) {
String strg = "Dog?Cat";
System.out.println("String: " + strg);
System.out.println("Char: " + strg.indexOf('?'));
System.out.println("String: " + strg.indexOf(new String("?")));
System.out.println("Sign: " + String.valueOf('\u0134'));
System.out.println("Unicode: " + strg.indexOf(String.valueOf('\u0134')));
}
}

结果:

String: Dog?Cat
Char: 3
String: 3
Sign: ?
Unicode: -1 --> Why is it -1 ?! Why not 3 ?

你能帮帮我吗?谢谢!

最佳答案

String.valueOf('\u0134') 不是 '?' 字符。 System.out.println 为许多不可打印的字符(例如 '\u0134')打印 '?',但是你的 "Dog ?Cat" 字符串不包含 '\u0134' 字符。

这会给你预期的 3 :

String strg = "Dog?Cat";
System.out.println("Unicode: " + strg.indexOf(String.valueOf('\u003f')));

关于java - 为什么 indexOf 没有给出来自 unicode 的正确结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com