Java 一个字符串在字典上比另一个字符串大是什么意思?

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

What does it mean a string is lexicographically greater than another string?

javastring

提问by Blerta Dhimitri

I have two strings in java:

我在java中有两个字符串:

s1 = "-7ff9";
s2 = "-7ff9";

I get the s2 string from a response from a server. Instead string s1 is a value of a parameter. When I ask :

我从服务器的响应中获取 s2 字符串。相反,字符串 s1 是一个参数值。当我问:

if(s1.equals(s2)){ System.out.println("YES");}
else System.out.println("NO");

I get the response NO.

我得到响应NO

I then compared the to strings:

然后我比较了字符串:

System.out.println(s1.compareTo(s2));

I get an 19output which means " string s1 is lexicographically greater than the string argument s2"

我得到一个 19输出,这意味着“字符串 s1 在字典上大于字符串参数 s2”

I just don't know how to solve this ...

我只是不知道如何解决这个问题...

采纳答案by Blerta Dhimitri

I solved my problem using :

我使用以下方法解决了我的问题:

if (s1.contentEquals(s2))

回答by Dr. Debasish Jana

You are comparing references, not teh contents. .equals() tests for value equality. For ignoring case, use equalsIgnoreCase(). String's compareTo Compares two strings lexicographically, means as if in a disctionary comparison

您正在比较参考文献,而不是内容。.equals() 测试值相等。对于忽略大小写,请使用 equalsIgnoreCase()。String's compareTo 按字典顺序比较两个字符串,就像在字典比较中一样

回答by Bohemian

"Lexographically greater" means it would appear afterthe other String if sorted by the unicode value of its (left-justified) characters.

“字典顺序更大”意味着如果按其(左对齐)字符的 unicode 值排序,它将出现另一个字符串之后

Just a guess, but it's the only reason I can think of that explains what you're seeing: The two minus signs are not in fact the same character. There are several characters that look (are printed) similar, but they are different unicode characters.

只是一个猜测,但这是我能想到的唯一原因来解释你所看到的:这两个减号实际上不是同一个字符。有几个字符看起来(打印)相似,但它们是不同的 unicode 字符。

See this linkfor the many characters that are considered a "dash".

有关被视为“破折号”的许多字符,请参阅此链接