Java 中的字符串比较
“按字典顺序比较两个字符串”是什么意思?
What does "compare two strings lexicographically" mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
“按字典顺序比较两个字符串”是什么意思?
What does "compare two strings lexicographically" mean?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
从 @Bozho 和 @aioobe 的答案来看,词典比较类似于人们在字典中可能找到的顺序。
Java String 类提供了
.compareTo ()
方法以便按字典顺序比较字符串。它的使用方式如下"apple".compareTo ("banana")
。该方法的返回值是一个
int
,可以解释如下:int
。 0 则调用该方法的字符串按字典顺序排在第一位(在字典中排在第一位)更具体地说,该方法提供了 ASCII 值中的第一个非零差值。
因此,
"computer".compareTo ("comparison")
将返回值(int) 'u' - (int) 'a'
(20)。由于这是一个肯定的结果,因此参数 ("comparison"
) 按字典顺序排在第一位。还有一个变体
.compareToIgnoreCase ()
,例如,对于"a".compareToIgnoreCase ("A");
,它将返回0
。Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary.
The Java String class provides the
.compareTo ()
method in order to lexicographically compare Strings. It is used like this"apple".compareTo ("banana")
.The return of this method is an
int
which can be interpreted as follows:compareTo
method is lexicographically first.More specifically, the method provides the first non-zero difference in ASCII values.
Thus
"computer".compareTo ("comparison")
will return a value of(int) 'u' - (int) 'a'
(20). Since this is a positive result, the parameter ("comparison"
) is lexicographically first.There is also a variant
.compareToIgnoreCase ()
which will return0
for"a".compareToIgnoreCase ("A");
for example.“比较”这个词有轻微的误导性。您不是在比较严格相等,而是比较哪个字符串在字典(词典)中排在第一位。
此功能允许对字符串集合进行排序。
请注意,这非常取决于活动区域设置。例如,在丹麦,我们有一个字符“å”,曾经被拼写为“aa”,并且与两个单个的 a 非常不同(编辑:如果发音为 “一个”!)。因此,丹麦排序规则将两个连续的 a 视为“å”,这意味着它在 z 之后。这也意味着丹麦语词典的排序方式与英语或瑞典语词典不同。
The wording "comparison" is mildly misleading. You are not comparing for strict equality but for which string comes first in the dictionary (lexicon).
This is the feature that allows collections of strings to be sortable.
Note that this is very dependent on the active locale. For instance, here in Denmark we have a character "å" which used to be spelled as "aa" and is very distinct from two single a's (EDIT: If pronounced as "å"!). Hence Danish sorting rules treat two consequtive a's identically to an "å", which means that it goes after z. This also means that Danish dictionaries are sorted differently than English or Swedish ones.
String.compareTo(..)
方法执行字典顺序比较。按字典顺序 == 按字母顺序。The
String.compareTo(..)
method performs lexicographical comparison. Lexicographically == alphebetically.按顺序比较具有相同位置的字母......更像是如何在字典中排序单词
Comparing sequencially the letters that have the same position against each other.. more like how you order words in a dictionary
如果您检查哪个字符串在词典中排在第一位,那么您就已经完成了字符串的词典比较!
一些链接:
被盗来自后一个链接:
If you check which string would come first in a lexicon, you've done a lexicographical comparison of the strings!
Some links:
Stolen from the latter link:
Java 字典顺序:
这看起来很奇怪,但确实如此...
我必须编写比较器链才能更改默认行为。
使用以下代码片段以及更好的输入字符串示例来验证顺序(您将需要 JSE 8):
结果
1Bmbiza
本杰明
海莉
杰基
坎比兹
萨曼莎
k1ambiz
kambiz
请注意,这是特定于区域设置的答案。
请注意,我正在过滤包含小写字母 a 的名称。
Java lexicographically order:
Odd as this seems, it is true...
I have had to write comparator chains to be able to change the default behavior.
Play around with the following snippet with better examples of input strings to verify the order (you will need JSE 8):
Result
1Bmbiza
Benjamin
Hayley
Jakey
Kambiz
Samantha
k1ambiz
kambiz
Please note this is answer is Locale specific.
Please note that I am filtering for a name containing the lowercase letter a.
下面的算法“按字典顺序比较两个字符串”
输入两个字符串 string 1 和 string 2。
for (int i = 0; i < str1.length( )&&
我< str2.length();我++)
(循环遍历两个字符
字符串比较它们直到 1
字符串终止):
a.如果两个字符的 unicode 值
相同则继续;
b.如果字符的 unicode 值
字符串 1 和字符串 2 的 unicode 值
不同则 return (str1[i]-str2[i])
如果字符串 1 的长度小于 string2
返回str2[str1.length()]
其他
返回str1[str2.length()]
//该方法按字典顺序比较两个字符串
来源: - 来源
Below Algo "compare two strings lexicographically"
Input two strings string 1 and string 2.
for (int i = 0; i < str1.length() &&
i < str2.length(); i ++)
(Loop through each character of both
strings comparing them until one
of the string terminates):
a. If unicode value of both the characters
is same then continue;
b. If unicode value of character of
string 1 and unicode value of string 2
is different then return (str1[i]-str2[i])
if length of string 1 is less than string2
return str2[str1.length()]
else
return str1[str2.length()]
// This method compares two strings lexicographically
Source : - Source
您可能还会遇到这样的任务,您必须“手动”实现字典比较,而不是使用默认的
compareTo()
方法。下面的简单算法基于比较后续位置的字符的 Unicode 值。
You might also come across a task, where you have to implement the lexicographical comparison "manually", not using the default
compareTo()
method.The below simple algorithm is based on comparing the Unicode value of chars at subsequent positions.