String: compareTo(String stringValue)
int compareTo(String anotherString)
- Compares two strings lexicographically.
int compareToIgnoreCase(String str)
- Compares two strings lexicographically, ignoring case differences.
The result of the compareTo(String anotherString)
and compareToIgnoreCase(String str)
is shown here:
Value | Meaning |
---|---|
Less than zero | The invoking string is less than str. |
Greater than zero | The invoking string is greater than str. |
Zero | The two strings are equal. |
public class Main {
public static void main(String[] argv) {
String str = "Java2s.com";
String str2 = "java2s.com";
System.out.println(str.compareTo(str2));
System.out.println(str.compareToIgnoreCase(str2));
}
}
The output:
-32
0
compareToIgnoreCase(String str)
returns the same results as compareTo( )
.
It only ignores the case differences.
public class Main {
public static void main(String args[]) {
System.out.println("A".compareToIgnoreCase("a"));
}
}
The output:
0
Home
Java Book
Essential Classes
Java Book
Essential Classes
String:
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String