Java Reference
In-Depth Information
Note that the index of the first character H is 0 (zero), the second character E is 1, and so on. The index of the last
character O is 4, which is equal to the length of the string " Hello" minus 1.
The following snippet of code will print the index value and the character at each index in a string of " HELLO" :
String str = "HELLO";
// Get the length of string
int len = str.length();
// Loop through all characters and print their indexes
for (int i = 0; i < len; i++) {
System.out.println(str.charAt(i) + " has index " + i);
}
H has index 0
E has index 1
L has index 2
L has index 3
O has index 4
Testing Strings for Equality
If you want to compare two strings for equality ignoring their cases, you can use the equalsIgnoreCase() method.
If you want to perform a case-sensitive comparison for equality, you need to use the equals() method instead as
previously described.
String str1 = "Hello";
String str2 = "HELLO";
if (str1.equalsIgnoreCase(str2)) {
System.out.println ("Ignoring case str1 and str2 are equal");
}
else {
System.out.println("Ignoring case str1 and str2 are not equal");
}
if (str1.equals(str2)) {
System.out.println("str1 and str2 are equal");
}
else {
System.out.println("str1 and str2 are not equal");
}
Ignoring case str1 and str2 are equal
str1 and str2 are not equal
 
Search WWH ::




Custom Search