How to work with the String class.

The associated package:

  • java.util.String

Common constructors of the String class:

Constructors Description
String() Creates an empty string ("")
String(arrayName) Creates a string from an array of char or byte types
String(ArranName, intOffset, intLength) Creates a string from a subset of an array or char or byte types.

Common methods of the String classes:

Method Description
length() Returns the length of this string
indexOf(String) Returns the index within this string of the first occurrence of the specified substring. If the string isn't found, this method returns -1.
indexOf(String, startIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If the string isn't found, this method returns -1.
lastIndexOf(String) Returns the index within this string of the last occurrence of the specified character
lastIndexOf(String, startIndex) Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index
trim() Returns a copy of the string, with leading and trailing whitespace omitted.
substring(startIndex) Returns a new string that is a substring of this string.
substring(startIndex, endIndex) Returns a new string that is a substring of this string
replace(startChar,endChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar
split(delimiter) Splits this string around matches of the given regular expression
charAt(index) Returns the char value at the specified index

The Compare methods of the String classes:

Method Description
equals(String) Compares this string to the specified object
equalsIgnoreCase(String) Compares this String to another String, ignoring case considerations.
startsWith(String) Tests if this string starts with the specified prefix
startsWith(String, startIndex) Tests if the substring of this string beginning at the specified index starts with the specified prefix
endsWith(String) Tests if this string ends with the specified suffix.
isEmpty() Returns true if, and only if, length() is 0
comparteTo(String) Compares two strings lexicographically.
compareToIgnoreCase(String) Compares two strings lexicographically, ignoring case differences

Descriptions:

  • For the third constructor shown above, the characters referred to by the intOffset and IntLength arguments must fall within the arry. Otherwise an IndexOutOfBoundsException will be thrown.
  • A char data type contains a single Unicode character. To code a literal char value, you use single quotes instead of double quotes.

Example 1: Two ways to create an empty string

String name = "";
String name = new String()

Example 2: Two ways to create a string from another string

String title = "This is a string";
String title = bookTitle;

Example 3: Two ways to create a string from an array of characters

char cityArray[] = {'H', 'e', 'l', 'l', 'o'};
String city1 = new String(cityArray); // HELLO
String city1 = new String(cityArray, 0, 3); // HELL

More detail can be found at: http://www.j2ee.me/javase/6/docs/api/java/lang/String.html#String()