Java 中的字符串比较

发布于 2024-09-30 08:58:56 字数 27 浏览 5 评论 0原文

“按字典顺序比较两个字符串”是什么意思?

What does "compare two strings lexicographically" mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

孤蝉 2024-10-07 08:58:56

从 @Bozho 和 @aioobe 的答案来看,词典比较类似于人们在字典中可能找到的顺序。

Java String 类提供了 .compareTo () 方法以便按字典顺序比较字符串。它的使用方式如下 "apple".compareTo ("banana")

该方法的返回值是一个int,可以解释如下:

  • returns int。 0 则调用该方法的字符串按字典顺序排在第一位(在字典中排在第一位)
  • 返回 == 0 则这两个字符串按字典顺序等效
  • 返回 > 0 则传递给compareTo方法的参数按字典顺序排在第一位。

更具体地说,该方法提供了 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:

  • returns < 0 then the String calling the method is lexicographically first (comes first in a dictionary)
  • returns == 0 then the two strings are lexicographically equivalent
  • returns > 0 then the parameter passed to the 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 return 0 for "a".compareToIgnoreCase ("A"); for example.

燃情 2024-10-07 08:58:56

“比较”这个词有轻微的误导性。您不是在比较严格相等,而是比较哪个字符串在字典(词典)中排在第一位。

此功能允许对字符串集合进行排序。

请注意,这非常取决于活动区域设置。例如,在丹麦,我们有一个字符“å”,曾经被拼写为“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.

浮萍、无处依 2024-10-07 08:58:56

String.compareTo(..) 方法执行字典顺序比较。按字典顺序 == 按字母顺序。

The String.compareTo(..) method performs lexicographical comparison. Lexicographically == alphebetically.

青芜 2024-10-07 08:58:56

按顺序比较具有相同位置的字母......更像是如何在字典中排序单词

Comparing sequencially the letters that have the same position against each other.. more like how you order words in a dictionary

三寸金莲 2024-10-07 08:58:56

如果您检查哪个字符串在词典中排在第一位,那么您就已经完成了字符串的词典比较!

一些链接:

被盗来自后一个链接:

如果按字典顺序,字符串 s 位于字符串 t 之前

  • st 的前缀,或
  • 如果cd分别是st的第一个字符,其中s t 不同,则 c 按字符顺序位于 d 之前。

注意:对于字母字符,字符顺序与字母顺序一致。数字在字母之前,大写字母在小写字母之前。

示例:

  • 房子先于家庭
  • 家庭先于房屋
  • 作曲家先于计算机
  • H2O 先于 HOTEL

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:

A string s precedes a string t in lexicographic order if

  • s is a prefix of t, or
  • if c and d are respectively the first character of s and t in which s and t differ, then c precedes d in character order.

Note: For the characters that are alphabetical letters, the character order coincides with the alphabetical order. Digits precede letters, and uppercase letters precede lowercase ones.

Example:

  • house precedes household
  • Household precedes house
  • composer precedes computer
  • H2O precedes HOTEL
裸钻 2024-10-07 08:58:56

Java 字典顺序:

  1. 数字 - 之前
  2. - 大写 - 之前
  3. - 小写

这看起来很奇怪,但确实如此...
我必须编写比较器链才能更改默认行为。
使用以下代码片段以及更好的输入字符串示例来验证顺序(您将需要 JSE 8):

import java.util.ArrayList;

public class HelloLambda {

public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<>();
    names.add("Kambiz");
    names.add("kambiz");
    names.add("k1ambiz");
    names.add("1Bmbiza");
    names.add("Samantha");
    names.add("Jakey");
    names.add("Lesley");
    names.add("Hayley");
    names.add("Benjamin");
    names.add("Anthony");

    names.stream().
        filter(e -> e.contains("a")).
        sorted().
        forEach(System.out::println);
}
}

结果

1Bmbiza
本杰明
海莉
杰基
坎比兹
萨曼莎
k1ambiz
kambiz

请注意,这是特定于区域设置的答案。
请注意,我正在过滤包含小写字母 a 的名称。

Java lexicographically order:

  1. Numbers -before-
  2. Uppercase -before-
  3. Lowercase

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):

import java.util.ArrayList;

public class HelloLambda {

public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<>();
    names.add("Kambiz");
    names.add("kambiz");
    names.add("k1ambiz");
    names.add("1Bmbiza");
    names.add("Samantha");
    names.add("Jakey");
    names.add("Lesley");
    names.add("Hayley");
    names.add("Benjamin");
    names.add("Anthony");

    names.stream().
        filter(e -> e.contains("a")).
        sorted().
        forEach(System.out::println);
}
}

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.

对不⑦ 2024-10-07 08:58:56

下面的算法“按字典顺序比较两个字符串”

  1. 输入两个字符串 string 1 和 string 2。

  2. for (int i = 0; i < str1.length( )&&
    我< str2.length();我++)

    (循环遍历两个字符
    字符串比较它们直到 1
    字符串终止):

    a.如果两个字符的 unicode 值
    相同则继续;

    b.如果字符的 unicode 值
    字符串 1 和字符串 2 的 unicode 值
    不同则 return (str1[i]-str2[i])

  3. 如果字符串 1 的长度小于 string2

    返回str2[str1.length()]

    其他

    返回str1[str2.length()]

    //该方法按字典顺序比较两个字符串

    public static int CompareCustom(String s1, String s2) {
        for (int i = 0; i < s1.length() && i< s2.length(); i++) {
            if(s1.charAt(i) == s2.charAt(i)){
                //System.out.println("等于");
                继续;
            }
            别的{
                返回 s1.charAt(i) - s2.charAt(i);
            }   
        }
        if(s1.length()s2.length()){
            返回 s1.length()-s2.length();
        }
        别的{
            返回0;
        }
    }
    

如果两个字符串相等则返回 0,否则返回负数或
正值

来源: - 来源

Below Algo "compare two strings lexicographically"

  1. Input two strings string 1 and string 2.

  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])

  3. if length of string 1 is less than string2

    return str2[str1.length()]

    else

    return str1[str2.length()]

    // This method compares two strings lexicographically

    public static int compareCustom(String s1, String s2) {
        for (int i = 0; i < s1.length() && i< s2.length(); i++) {
            if(s1.charAt(i) == s2.charAt(i)){
                //System.out.println("Equal");
                continue;
            }
            else{
                return s1.charAt(i) - s2.charAt(i);
            }   
        }
        if(s1.length()<s2.length()){
            return s2.length() - s1.length();
        }
        else if(s1.length()>s2.length()){
            return s1.length()-s2.length();
        }
        else{
            return 0;
        }
    }
    

if two String are equal it will return 0 otherwise return Negative or
positive value

Source : - Source

故人爱我别走 2024-10-07 08:58:56

您可能还会遇到这样的任务,您必须“手动”实现字典比较,而不是使用默认的 compareTo() 方法。

下面的简单算法基于比较后续位置的字符的 Unicode 值。

@Override
public int compareTo(Person otherPerson) {
        
// Getters, constructor, variables ... 

        int result = 0;

            for (int i = 0; i < getName().length() && i < otherPerson.getName().length(); i++) {
                if (getName().charAt(i) > otherPerson.getName().charAt(i)) {
                    result = 1;
                    break;
                } else if (getName().charAt(i) < otherPerson.getName().charAt(i)) {
                    result = -1;
                    break;
                }
            }
        }
        return result;
    }
}

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.

@Override
public int compareTo(Person otherPerson) {
        
// Getters, constructor, variables ... 

        int result = 0;

            for (int i = 0; i < getName().length() && i < otherPerson.getName().length(); i++) {
                if (getName().charAt(i) > otherPerson.getName().charAt(i)) {
                    result = 1;
                    break;
                } else if (getName().charAt(i) < otherPerson.getName().charAt(i)) {
                    result = -1;
                    break;
                }
            }
        }
        return result;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文