![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e71. Comparing StringsSee also e306 Comparing Strings in a Locale-Independent Way.String s1 = "a"; String s2 = "A"; String s3 = "B"; // Check if identical boolean b = s1.equals(s2); // false // Check if identical ignoring case b = s1.equalsIgnoreCase(s2); // true // Check order of two strings int i = s1.compareTo(s2); // 32; lowercase follows uppercase if (i < 0) { // s1 precedes s2 } else if (i > 0) { // s1 follows s2 } else { // s1 equals s2 } // Check order of two strings ignoring case i = s1.compareToIgnoreCase(s3); // -1 if (i < 0) { // s1 precedes s3 } else if (i > 0) { // s1 follows s3 } else { // s1 equals s3 } // A string can also be compared with a StringBuffer; // see e70 Constructing a String StringBuffer sbuf = new StringBuffer("a"); b = s1.contentEquals(sbuf); // true
e72. Determining If a String Contains a Substring e73. Getting a Substring from a String e74. Searching a String for a Character or a Substring e75. Replacing Characters in a String e76. Replacing Substrings in a String e77. Converting a String to Upper or Lower Case e78. Converting a Primitive Type Value to a String e79. Converting Between Unicode and UTF-8 e80. Determining a Character's Unicode Block e81. Determining If a String Is a Legal Java Identifier
© 2002 Addison-Wesley. |