Java Reference
In-Depth Information
TABLE 3-1 Some Commonly Used String Methods (continued)
int indexOf( char ch, int pos)
//Returns the index of the first occurrence of the character
//specified by ch; The parameter pos specifies where to
//begin the search; If the character specified by ch does not
//appear in the string, it returns -1
//Example: sentence.indexOf('a', 10) returns 18
3
int indexOf(String str)
//Returns the index of the first occurrence of the string
//specified by str; If the string specified by str does not
//appear in the string, it returns -1
//Example: sentence.indexOf("with") returns 12
//
sentence.indexOf("ing") returns 8
int indexOf(String str, int pos)
//Returns the index of the first occurrence of the String
//specified by str; The parameter pos specifies where to begin
//the search; If the string specified by str does not appear
//in the string, it returns -1
//Example: sentence.indexOf("a", 10) returns 18
//
sentence.indexOf("Pr", 10) returns -1
String concat(String str)
//Returns the string that is this string concatenated with str
//Example: The expression
//
sentence.concat(" is fun.")
//
returns the string "Programming with Java is fun."
int compareTo(String str)
//Compares two strings character by character
//Returns a negative value if this string is less than str
//Returns 0 if this string is same as str
//Returns a positive value if this string is greater than str
boolean equals(String str)
//Returns true if this string is same as str
int length()
//Returns the length of the string
//Example: sentence.length() returns 21, the number of characters in
//
"Programming with Java"
String replace( char charToBeReplaced, char charReplacedWith)
//Returns the string in which every occurrence of
//charToBeReplaced is replaced with charReplacedWith
//Example: sentence.replace('a', '*') returns the string
//
"Progr*mming with J*v*"
//
Each occurrence of a is replaced with *
Search WWH ::




Custom Search