Bifalgorithm post · Computer science one · Comparisons

How do you compare strings in Java?

How do you compare strings in Java? (Computer science one · Comparisons) — "When comparing strings in Java, we cannot use the numerical comparison operators su…

Ideas in this flowchart

  • Java字符串的比较应依据比较方式和比较目的进行
  • 不能使用 == 或 < 等数值比较运算符直接比较字符串内容
  • 字符串是对象,因此这些运算符比较的是变量所引用的内存地址
  • 分别通过两个构造器创建的相同内容字符串通常指向不同的内存地址
  • 因此,即使 a 和 b 内容相同,a == b 也不会成立,示例不会打印任何内容
  • String 类提供 compareTo() 方法来进行字符串的字典序比较
  • compareTo() 返回负数、零或正数,分别表示调用字符串较小、相等或较大
  • 例如,apple.compareTo("banana") 为负数,zelda.compareTo("mario") 为正数,而 Hello.compareTo("Hello") 为零
  • 较短且作为前缀的字符串排在较长字符串之前,例如 apple 排在 apples 之前
  • 大写字母按照 ASCII 顺序排在小写字母之前,因此 Apple 排在 apple 之前
  • compareToIgnoreCase() 以忽略大小写的方式进行相同的字典序比较
  • equals() 只返回 true 或 false,用于判断字符串是否相同,不能提供相对顺序
  • 这些比较能力属于 String 类,因为字符串实现了定义字典序的 Comparable 接口
Feed/

How do you compare strings in Java?

No cover

Channel

Computer science one

1 page

"When comparing strings in Java, we cannot use the numerical comparison operators such as ==, or <. Because strings are objects, using these operators actually compares the variables’ memory addresses.\n\nString a = new String(\"Hello World!\");\n\nString b = new String(\"Hello World!\");\n\nif(a == b) {\n\nSystem.out.println(\"strings match!\");\n\n}\n\nThe code above will not print anything even though the strings a and b have the same content. This is because a == b is comparing the memory address of the two variables. Since they point to different memory addresses (created by two separate calls to the constructors) they will not be equal. Instead, the String class provides a compareTo() method that takes another string and returns something negative, zero, or something positive depending on the relative lexicographic ordering of the strings.\n\nint x;\n\nString a = \"apple\";\n\nString b = \"zelda\";\n\nString c = \"Hello\";\n\nx = a.compareTo(\"banana\"); //x is negative\n\nx = b.compareTo(\"mario\"); //x is positive\n\nx = c.compareTo(\"Hello\"); //x is zero\n\n//shorter strings precede longer strings:\n\nx = a.compareTo(\"apples\"); //x is negative\n\nString d = \"Apple\";\n\nx = d.compareTo(\"apple\"); //x is negative\n\nIn the last example, \"Apple\" precedes \"apple\" since uppercase letters are ordered before lowercase letters according to the ASCII table. We can also make case-insensitive comparisons using the compareToIgnoreCase() method which works the same way. Here, d.compareToIgnoreCase(\"apple\") will return zero as the two strings are the same ignoring the cases. Note that the commonly used equals() method only returns true or false depending on whether or not two strings are the same or different. They cannot be used to provide a relative ordering of two strings. This method is part of the String class due to the fact that strings implement the Comparable interface which defines a lexicographic ordering."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.