>
Click HERE for the Substring Methods of String class.
The String Class: (Part 2)
Comparison Methods of String class:

- Example: (equals)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
/* == cannot be used to check whether two strings has same content or not. == only checks whether the strings are objects of String class or not. To check the content equality, equals() method is used. */ class test { public static void main(String ar[]) { String str1 = "Welcome to Java"; String str2 = "Welcome to Java"; String str3 = new String("Welcome to Java"); System.out.println(str1==str2); // true. because str1 and str2 are of variables of same data type ->String System.out.println(str1==str3); // false. because, str1 is a varaible of type String and str3 is an object of class String. System.out.println(str1.equals(str2)); // true. because equals checks the content and the content of str1 and str2 is same. System.out.println(str1.equals(str3)); // true. content of str1 and str3 is the same. } } - Example: (equalsIgnoreCase)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Java PROGRAMMING"; String str2 = "java programming"; System.out.println(str1.equalsIgnoreCase(str2)); // true } } - Example: (compareTo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "A"; String str2 = "a"; System.out.println(str1.compareTo(str2)); // (65-97 = -32)(A-a) String str3 = "Demo"; String str4 = "Eemo"; System.out.println(str3.compareTo(str4)); // (68-69 = -1)(D-E) String str5 = "ABCF"; String str6 = "ABAI"; System.out.println(str5.compareTo(str6)); /* check character 1: A-A=0 check character 2: B-B=0 check character 3: C-A=67-65 =2 (as soon as the score is greater then 0, no further check is done. And the last score is printed. So this prints 2) */ } } - Example: (compareToIgnoreCase)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "A"; String str2 = "a"; System.out.println(str1.compareToIgnoreCase(str2)); //removes the case sensitivity and then checks. So this prints 0 String str3 = "ABc"; String str4 = "abC"; System.out.println(str3.compareToIgnoreCase(str4)); //removes the case sensitivity and then checks. So this prints 0 String str5 = "b"; String str6 = "A"; System.out.println(str5.compareToIgnoreCase(str6)); //removes the case sensitivity of str6 and then checks. Computes: b-a. prints 1 String str7 = "A"; String str8 = "b"; System.out.println(str7.compareToIgnoreCase(str8)); //removes the case sensitivity of str8 and then checks. Computes: A-B. prints -1 } } - Example: (startsWith)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Hello!"; String str2 = "h"; System.out.println(str1.startsWith(str2)); //false. h is lowercase String str3 = "Hello!"; String str4 = "H"; System.out.println(str3.startsWith(str4)); // true String str5 = "Good Morning!"; String str6 = "Good"; System.out.println(str5.startsWith(str6)); // true } } - Example: (endsWith)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Hello!"; String str2 = "o"; System.out.println(str1.endsWith(str2)); //false. 'o' is not the lastmost character String str3 = "Hello!"; String str4 = "!"; System.out.println(str3.endsWith(str4)); // true. '!' is the lastmost character String str5 = "Good Morning!"; String str6 = "G!"; System.out.println(str5.endsWith(str6)); // false. this string ends with g! and not with G! String str7 = "Good Morning!"; String str8 = "g!"; System.out.println(str7.endsWith(str8)); // true } } - Example: (contains)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Morning!"; System.out.println(str1.contains("good")); //false. G is uppercase System.out.println(str1.contains("Good")); //true System.out.println(str1.contains("!")); //true System.out.println(str1.contains("nin")); //true } }
Click HERE for the Substring Methods of String class.
Comments
Post a Comment