Computer Applications
Differentiate between compareTo() and compareToIgnore().
Java String Handling
3 Likes
Answer
compareTo() | compareToIgnore() |
---|---|
It compares two strings lexicographically. | It compares two strings lexicographically, ignoring the case of the characters in a string. |
Example: String str1 = "computer"; String str2 = "COMPUTER"; int res = str1.compareTo(str2); System.out.println(res); The output is 32 as 'c' and 'C' are treated differently. | Example: String str1 = "computer"; String str2 = "COMPUTER"; int res = str1.compareToIgnore(str2); System.out.println(res); The output is 0 as case difference is ignored. |
Answered By
3 Likes
Related Questions
Differentiate between indexOf() and lastIndexOf().
Write a program to input a sentence and arrange each word of the string in alphabetical order.
Differentiate between startsWith() and endsWith().
Write a program to input a sentence and print each word of the string along with its length in tabular form.