Strings

Characters and Strings
Constructors
String Length
Individual Characters
String Comparisons
Locating Substrings
String Extraction
String Concatenation
Miscellaneous Methods
StringTokenizer

Characters and Strings

Characters

Strings

 


String Constructors


String Length

int length( ) -- determines the number of characters in a String


Individual Characters

char charAt(int index) - returns the character at a specified position in the String

void getChars(int srcBegin, int srcEnd, char[ ] dst, int dstBegin) - copies characters from this string into the destination character array.


Comparing Strings

All characters are represented as numeric codes, and lexicographic comparison of two strings compares the numeric codes of the individual characters in the strings. Example: the String "Hello" is not equal to the String "hello".

boolean equals ( Object ) - tests the calling object for equality with the object specified in the argument and returns true if the objects are equal and false otherwise.

 

boolean equalsIgnoreCase ( String ) - determines if the calling String is equal to argument String, ignoring the case.

int compareTo ( String ) - determines if the calling String is equal to the argument String (using lexicographical comparison)

boolean regionMatches ( boolean, int, String, int, int ) -compares portions of the calling String and a String specified in the argument for equality and returns true if the specified number of characters are lexicographically equal.

== When references are compared with = =, the result is true only if both references refer to the same object.

boolean startsWith ( String ) - determines if the calling String begins with the argument String.

boolean endsWith ( String ) - determines if the calling String ends with the argument String.


Locating Characters and Substrings in Strings

int indexOf ( String ) -- locates the first occurrence of a String in the calling String

int indexOf ( int ) -- locates the first occurrence of a character in the calling String

int lastIndexOf ( String ) - searches from the end of the String toward the beginning of the String to locate the last occurrence of a  String in a String, and returns the index if that String is found, otherwise -1 is returned.

int lastIndexOf ( int ) - searches from the end of the String toward the beginning of the String to locate the last occurrence of a character in a String, and returns the index of that character if the character is found, otherwise -1 is returned.



Extracting Substrings from Strings

String substring ( int ) - extracts a substring from the calling String.



Concatenating Strings

String concat ( String ) -- appends the String specified in the argument to the calling String and returns a new String object containing the characters from both original Strings.

Stings can also be concatenated using the + operator, which is actually faster: s1 = s1 + s2;


Miscellaneous String Methods

String replace ( char, char ) - replaces every occurrence in the calling String of the character specified by the first argument by the character specified in the second argument.

String toUpperCase ( ) -- generates a new String object with uppercase letters where corresponding lowercase letters reside in the calling String.

String toLowerCase ( ) - generates a new String object with lowercase letters where corresponding uppercase letters reside in the calling String.

 

String trim( ) -- removes all whitespace characters from the beginning or end of the calling String.

 

String valueOf ( ? ) -- takes arguments of various types, converts those arguments to strings and returns them as String objects.

 


StringTokenizer

String stringToTokenize = o.toString( ); -- creates a String reference stringToTokenize and assigns it o.toString( ) -- the text in the text field.

StringTokenizer tokens = new StringTokenizer( stringToTokenize ); -- creates a StringTokenizer object.

tokens.countTokens( ) -- determines the number of tokens in the String to be tokenized.

tokens.hasMoreTokens( ) -- determines if there are more tokens in the String being tokenized.

tokens.nextToken( ) - returns a String which contains the next token.

tokens.nextToken( newDelimiterString ); -- allows the delimiter String to be changed while tokenizing a String.


For more details, see the Java API.

See this link.