Java Examples - String Comparison
In the following example we compare two strings with the string functions compareTo (string), compareToIgnoreCase(String) and compareTo(object string), and return the ASCII difference of the first letter of the strings.
The example code is as follows:
StringCompareEmp.java file
public
class
StringCompareEmp
{
public
static
void
main
(
String
args
[
]
)
{
String
str
=
"
Hello World
"
;
String
anotherString
=
"
hello world
"
;
Object
objStr
=
str
;
System
.
out
.
println
(
str
.
compareTo
(
anotherString
)
)
;
System
.
out
.
println
(
str
.
compareToIgnoreCase
(
anotherString
)
)
;
//
Case-insensitive
System
.
out
.
println
(
str
.
compareTo
(
objStr
.
toString
(
)
)
)
;
}
}
The output of the above code example is:
-32
0
0
Java Examples