Java Reference
In-Depth Information
This results in a value of 4 in the variable x .Ifthe string holds no such character,
the method returns -1 .Tocontinue searching for more instances of the character,
you can use the method
indexOf (int ch, int fromIndex)
This starts the search at the fromIndex location in the string and searches to
the end of the string. The additional overloaded methods
indexOf (String str)
indexOf (String str, int fromIndex)
provide similar functions but search for a substring rather than just for a single
character. Similarly, the methods
lastIndexOf (int ch)
lastIndexOf (int ch, int fromIndex)
lastIndexOf (String str)
lastIndexOf (String str, int fromIndex)
search “backwards” for characters and strings starting from the right end and
moving from right to left. (The fromIndex second parameter still counts from
the left, with the search continuing from that index position toward the beginning
of the string.)
10.11.1.4 boolean startsWith (String prefix) , boolean
endsWith (String str)
These two methods test whether a string begins or ends with a particular substring.
Forexample,
String [] str ={"Abe", "Arthur", "Bob"};
for (int i=0; i < str.length (); i++) {
if (str[i].startsWith ("Ar")) doSomething ();
}
10.11.1.5 String toLowerCase () , String toUpperCase ()
The first method returns a new string with all the characters set to lower case
while the second returns a string with the characters set to upper case:
String[] str ={" Abe " , " Arthur " , " Bob "} ;
for (int i = 0; i < str.length (); i++) {
if (str[i].toLowerCase ().startsWith ( " ar " )) doSomething ();
}
See the Java 2 Platform API Specifications to examine the other methods available
with the String class [3].
Search WWH ::




Custom Search