Class String


  extended by Object
      extended by String

public class String
extends Object

The String class represents character strings. All string literals, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

See Also:
Buffer

Method Summary
 char charAt(int index)
          Returns the character at the specified index.
 int compareTo(String str)
          Compares two strings lexicographically.
 int compareToIgnoreCase(String str)
          Compares two strings lexicographically, ignoring case differences.
 boolean endsWith(String suffix)
          Tests if this string ends with the specified suffix.
 boolean equalsIgnoreCase(String str)
          Compares this String to another String, ignoring case considerations.
 List explode(String divider)
          Splits this String into list of substrings according to given divider string.
 int indexOf(char ch, int fromIndex)
          Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
 int indexOf(String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
 int lastIndexOf(char ch, int fromIndex)
          Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
 int length()
          Returns the length of this string.
 char operator_get(int index)
          Returns the character at the specified index.
 String replace(char oldChar, char newChar)
          Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
 boolean startsWith(String prefix)
          Tests if this string starts with the specified prefix.
 String substring(int beginIndex, int endIndex)
          Returns a new string that is a substring of this string.
 String toLowerCase()
          Converts all of the characters in this String to lower case.
 String toUpperCase()
          Converts all of the characters in this String to upper case.
 String trim()
          Removes white space from both ends of this string.
 
Methods inherited from class Object
toString, equals, hashCode
 
Methods inherited from
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

length

public int length()
Returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.

Returns:
the length of the sequence of characters represented by this object.

charAt

public char charAt(int index)
Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Parameters:
index - the index of the character.
Returns:
the character at the specified index of this string. The first character is at index 0.

substring

public String substring(int beginIndex,
                        int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Returns:
the specified substring.

startsWith

public boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.

Parameters:
prefix - the prefix.
Returns:
true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the Object.equals(Object) method.

endsWith

public boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.

Parameters:
suffix - the suffix.
Returns:
true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object as determined by the Object.equals(Object) method.
Throws:
java.lang.NullPointerException - if suffix is null.

indexOf

public int indexOf(String str,
                   int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

Parameters:
str - the substring to search for.
fromIndex - the index to start the search from.
Returns:
If the string argument occurs as a substring within this object at a starting index no smaller than fromIndex, then the index of the first character of the first such substring is returned. If it does not occur as a substring starting at fromIndex or beyond, -1 is returned.

indexOf

public int indexOf(char ch,
                   int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of the first such occurrence is returned--that is, the smallest value k such that:

 (this.charAt(k) == ch) && (k >= fromIndex)
 
is true. If no such character occurs in this string at or after position fromIndex, then -1 is returned.

There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

Parameters:
ch - a character.
fromIndex - the index to start the search from.
Returns:
the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.

lastIndexOf

public int lastIndexOf(char ch,
                       int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index. That is, the index returned is the largest value k such that:
 (this.charAt(k) == ch) && (k <= fromIndex)
 
is true.

Parameters:
ch - a character.
fromIndex - the index to start the search from. There is no restriction on the value of fromIndex. If it is greater than or equal to the length of this string, it has the same effect as if it were equal to one less than the length of this string: this entire string may be searched. If it is negative, it has the same effect as if it were -1: -1 is returned.
Returns:
the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to fromIndex, or -1 if the character does not occur before that point.

trim

public String trim()
Removes white space from both ends of this string.

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

Otherwise, if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned.

Otherwise, let k be the index of the first character in the string whose code is greater than '\u0020', and let m be the index of the last character in the string whose code is greater than '\u0020'. A new String object is created, representing the substring of this string that begins with the character at index k and ends with the character at index m-that is, the result of this.substring(km+1).

This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.

Returns:
this string, with white space removed from the front and end.

toUpperCase

public String toUpperCase()
Converts all of the characters in this String to upper case.

Returns:
the String, converted to uppercase.
See Also:
toLowerCase()

toLowerCase

public String toLowerCase()
Converts all of the characters in this String to lower case.

Returns:
the String, converted to lowercase.
See Also:
toUpperCase()

replace

public String replace(char oldChar,
                      char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Parameters:
oldChar - the old character.
newChar - the new character.
Returns:
a string derived from this string by replacing every occurrence of oldChar with newChar.

compareTo

public int compareTo(String str)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the Object.equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

 this.charAt(k)-str.charAt(k)
 
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
 this.length()-str.length()
 

Parameters:
str - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

compareToIgnoreCase

public int compareToIgnoreCase(String str)
Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by converting each character to lower case.

Parameters:
str - the String to be compared.
Returns:
a negative integer, zero, or a positive integer as the the specified String is greater than, equal to, or less than this String, ignoring case considerations.

equalsIgnoreCase

public boolean equalsIgnoreCase(String str)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.

Parameters:
str - the String to compare this String against.
Returns:
true if the argument is not null and the Strings are equal, ignoring case; false otherwise.

operator_get

public char operator_get(int index)
Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Parameters:
index - the index of the character.
Returns:
the character at the specified index of this string. The first character is at index 0.

explode

public List explode(String divider)
Splits this String into list of substrings according to given divider string.

Parameters:
divider - Divider string
Returns:
List of elements