gpt4 book ai didi

Java String/Char charAt() 比较

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:08:13 25 4
gpt4 key购买 nike

我看到了您可以使用 charAt() 方法进行的各种比较。

但是,我真的不能理解其中的一些。

String str = "asdf";
str.charAt(0) == '-'; // What does it mean when it's equal to '-'?


char c = '3';
if (c < '9') // How are char variables compared with the `<` operator?

如有任何帮助,我们将不胜感激。

最佳答案

// What does it mean when it's equal to '-'?

每个字母和符号都是一个字符。您可以查看字符串的第一个字符并检查是否匹配。

在这种情况下,您获取第一个字符并查看它是否是减号字符。这个减号是(char) 45见下文

// How are char variables compared with the < operator?

在Java中,所有字符实际上都是16位无符号数。每个字符都有一个基于它的 unicode 的数字。例如'9'是字符(char) 57对于小于 9 代码的任何字符,此比较都是正确的例如空间。

enter image description here

字符串的第一个字符是 'a'这是(char) 97所以(char) 97 < (char) 57是假的。

关于Java String/Char charAt() 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40085875/

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