Java String/Char charAt() 比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40085875/
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-11-03 04:54:55  来源:igfitidea点击:

Java String/Char charAt() Comparison

javacharcomparisoncharat

提问by Kcits

I have seen various comparisons that you can do with the charAt()method.

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

However, I can't really understand a few of them.

但是,我无法真正理解其中的一些。

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?

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Peter Lawrey

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

// 当它等于'-'时是什么意思?

Every letter and symbol is a character. You can look at the first character of a String and check for a match.

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

In this case you get the first character and see if it's the minus character. This minus sign is (char) 45see below

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

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

// char 变量与<运算符相比如何?

In Java, all characters are actually 16-bit unsigned numbers. Each character has a number based on it unicode. e.g. '9'is character (char) 57This comparison is true for any character less than the code for 9e.g. space.

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

enter image description here

在此处输入图片说明

The first character of your string is 'a'which is (char) 97so (char) 97 < (char) 57is false.

你的字符串的第一个字符 is 'a'which is (char) 97so (char) 97 < (char) 57is false。

回答by almost a beginner

String str = "asdf";
String output = " ";
if(str.charAt(0) == '-'){
  // What does it mean when it's equal to '-'?
  output= "- exists in the first index of the String";
}
else {
    output="- doesn't exists in the first index of the String"; 
}
System.out.println(output);

It checks if that char exists in index 0, it is a comparison.

它检查该字符是否存在于索引 0 中,这是一个比较。

As for if (c < '9'), the ascii values of c and 9 are compared. I don't know why you would check if ascii equivalent of c is smaller than ascii equivalent of '9' though.

至于if (c < '9'),比较了 c 和 9 的 ascii 值。我不知道你为什么要检查 c 的 ascii 等效值是否小于 '9' 的 ascii 等效值。

If you want to get ascii value of any char, then you can:

如果你想获得任何字符的 ascii 值,那么你可以:

char character = 'c';
int ascii = character;
System.out.println(ascii);

回答by Tilak Maddy

str.charAt(0) == '-';returns a boolean , in this case false.

str.charAt(0) == '-';返回一个布尔值,在这种情况下false

if (c < '9')compares ascii value of '3' with ascii value of '9' and return boolean again.

if (c < '9')将 '3' 的 ascii 值与 '9' 的 ascii 值进行比较并再次返回布尔值。

回答by Nyakiba

str.charAt(0) == '-'

This statement returns a true if the character at point 0 is '-' and false otherwise.

如果点 0 处的字符是“-”,则此语句返回真,否则返回假。

if (c < '9')

This compares the ascii value of c with the ascii value of '9' in this case 99 and 57 respectively.

在这种情况下,这将分别将 c 的 ascii 值与 '9' 的 ascii 值进行比较,分别为 99 和 57。

回答by Roc Aràjol

Characters are a primitive type in Java, which means it is not a complex object. As a consequence, every time you're making a comparison between chars, you are directly comparing their values.

字符在 Java 中是一种原始类型,这意味着它不是一个复杂的对象。因此,每次在 之间进行比较时chars,您都是在直接比较它们的值。

Java characters are defined according to the original unicode specification, which gives each character a 16-bit value. These are the values that Java is comparing when you are comparing something like c>'3'or str.charAt(0) == '-'.

Java 字符是根据原始 unicode 规范定义的,它为每个字符提供一个 16 位值。这些是 Java 在比较类似c>'3'或时比较的值str.charAt(0) == '-'