The String Class
is a sequence of characters.
is actually a predefined class in the Java library.
Comparing Strings
String s1 = "Welcome to Java";
String s2 = "Welcome to Java";
String s3 = "Welcome to C";
Method Description
equals(s1) Returns true if this string is equal to string s1.
e.g.,
s1.equals(s2); // true
s1.equals(s3); // false
equalsIgnoreCase(s1) Returns true if this string is equal to string s1; it is case insensitive.
s1.equalsIgnoreCase(s2); // true
s1.equalsIgnoreCase(s3); // false
compareTo(s1) Returns an integer greater than 0, equal to 0, or less than 0 to indicate
whether this string is greater than, equal to, or less than s1.
e.g.
s1.compareTo(s2); // 0
s1.compareTo(s3); // 7
compareToIgnoreCase(s1) Same as compareTo except that the comparison is case insensitive.
e.g.
s1.compareToIgnoreCase(s2); // 0
s1.compareToIgnoreCase(s3); // 7
startsWith(prefix) Returns true if this string starts with the specified prefix.
e.g.,
s1.startsWith("We"); // true
s1.startsWith("we"); // false
endsWith(suffix) Returns true if this string ends with the specified suffix.
e.g.,
s1.endsWith("Java"); // true
s1.endsWith("java"); // false
contains(s1) Returns true if s1 is a substring in this string.
e.g.,
s1.contains("to"); // true
s1.contains("To"); // false
On this page of the site you can watch the video online Java Tutorial 52 - The String Class 4-5 with a duration of hours minute second in good quality, which was uploaded by the user Ken 04 February 2020, share the link with friends and acquaintances, this video has already been watched 16 times on youtube and it was liked by 1 viewers. Enjoy your viewing!