Java Reference
In-Depth Information
Display 1.4 Some Methods in the Class String (part 3 of 4)
String substring( Start , End )
Returns the substring of the calling object string starting from position Start through, but not includ-
ing, position End of the calling object. Positions are counted 0, 1, 2, etc. Be sure to notice that the
character at position Start is included in the value returned, but the character at position End is not
included.
EXAMPLE
After program executes String sample = "AbcdefG";
sample.substring(2, 5) returns "cde" .
int indexOf( A_String )
Returns the index (position) of the first occurrence of the string A_String in the calling object string.
Positions are counted 0, 1, 2, etc. Returns 1 if A_String is not found.
EXAMPLE
After program executes String greeting = "Hi Mary!";
greeting.indexOf("Mary") returns 3 , and
greeting.indexOf("Sally") returns 1 .
int indexOf( A_String , Start )
Returns the index (position) of the first occurrence of the string A_String in the calling object string
that occurs at or after position Start . Positions are counted 0, 1, 2, etc. Returns 1 if A_String is not
found.
EXAMPLE
After program executes String name = "Mary, Mary quite contrary";
name.indexOf("Mary", 1) returns 6.
The same value is returned if 1 is replaced by any number up to and including 6 .
name.indexOf("Mary", 0) returns 0.
name.indexOf("Mary", 8) returns 1.
int lastIndexOf( A_String )
Returns the index (position) of the last occurrence of the string A_String in the calling object string.
Positions are counted 0, 1, 2, etc. Returns 1 , if A_String is not found.
EXAMPLE
After program executes String name = "Mary, Mary, Mary quite so";
greeting.indexOf("Mary") returns 0 , and
name.lastIndexOf("Mary") returns 12 .
Search WWH ::




Custom Search