简体   繁体   English

比较两个字符串的Java风格

[英]Comparing Two Strings Java Style

I saw this in my readings that allows you to compare two strings using the compareToCaseIgnore(String). 我在阅读材料中看到了这一点,使您可以使用compareToCaseIgnore(String)比较两个字符串。

public class CompareWithoutCase
{
   public static void main(String [] args)
   {
        String name1, name2;  // To hold two names

        // Create a Scanner object to read input.
        Scanner keyboard = new Scanner(System.in);

        // Get a name.
        System.out.print("Enter a name: ");
        name1 = keyboard.nextLine();

        // Get another name.
        System.out.print("Enter another name: ");
        name2 = keyboard.nextLine();

        // Compare the names.
        if (name1.compareToIgnoreCase(name2) < 0)
        {
           System.out.println(name1 + " is less than " + name2);
        }
        else if (name1.compareToIgnoreCase(name2) == 0)
        {
           System.out.println(name1 + " is equal to " + name2);
        }
        else if (name1.compareToIgnoreCase(name2) > 0)
        {
           System.out.println(name1 + " is greater than " + name2);
        }
    }
}

My question is why would a string such as cli be different from ilc in value if they hold the same characters? 我的问题是,如果cli等字符串具有相同的字符,为什么它们的值会与ilc不同? I've read in another question that a lower case letter is different from a capital letter in value and I somewhat understand that, though not fully. 我在另一个问题中读到,小写字母与大写字母的价值不同,尽管有些不完整,但我还是有所理解。 Is there any place I can find an explanation for this in depth or can someone explain with a few examples? 我在任何地方都可以找到有关此问题的详细解释,或者有人可以通过一些示例进行解释吗? I input 我输入

cli CLI

for the first string and 对于第一个字符串和

ilc ILC

for the second name and got 为第二个名字,并得到

cli is less than ilc cli小于ilc

Why is one less than the other with the same letters in each string? 为什么每个字符串中的字母都比另一个少?

Lower case and upper case letters do have different ASCII values, but they are separated by a constant (42, I think). 小写字母和大写字母的确具有不同的ASCII值,但它们之间用一个常数分隔(我认为是42)。 That is to say, A is the same distance from a as B is from b as C is from c , etc. 也就是说, Aa距离与Bb距离相同, Ccc距离相同,等等。

ilc and cli are different in that they're completely different characters. ilccli的不同之处在于它们是完全不同的字符。

compareToIgnoreCase (or equalsIgnoreCase ) does a character by character comparison. compareToIgnoreCase (或equalsIgnoreCase )按字符进行比较。 That is, it compares the character at index 0 of str1 to the character at index 0 of str2 . 即,它将str1索引0处的字符与str2索引0处的字符进行比较。 And so on, for every index of the two strings being compared. 依此类推,对于要比较的两个字符串的每个索引。

If you compare ilc and cli , then i in ilc is compared to c in cli (and they're different), so the comparison returns false . 如果你比较ilccli ,然后iilc相比ccli (和他们是不同的),因此比较返回false The comparison doesn't care if i appears elsewhere in the second string, it only cares if i or I exists in the second string at the same index it exists in the first string (and so on for all the rest of the characters, though I imagine it's probably optimized to return false as soon as it finds one difference). 比较并不关心i出现在第二个字符串的其他地方,它只关心iI存在于第二个字符串中,且索引与第一个字符串中存在的索引相同(对于所有其他字符,依此类推)我想如果发现一个差异,它可能会被优化为return false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM