Java Reference
In-Depth Information
tion, the index of 19. The third example specifies only one argument; therefore, the res-
ult will be the original string beginning with the position specified by that argument.
Note The substring() method only accepts positive integer values. If you at-
tempt to pass a negative value, an exception will be thrown.
3-2. Comparing Strings
Problem
An application that you are writing needs to have the ability to compare two or more
string values.
Solution
Use the built-in equals() , equalsIgnoreCase() , compareTo() , and com-
pareToIgnoreCase() methods to compare the values contained within the strings.
The following is a series of tests using different string-comparison operations.
As you can see, various if statements are used to print out messages if the compar-
isons are equal:
String one = "one";
String two = "two";
String var1 = "one";
String var2 = "Two";
String pieceone = "o";
String piecetwo = "ne";
// Comparison is equal
if (one.equals(var1)){
System.out.println ("String one equals var1 using
equals");
}
Search WWH ::




Custom Search