String Class

Constructing a String
String firstName = new String ("Alan ");
String lastName = "Turing";

String Length
int length = firstName.length();

Accessing characters in a String
The characters in a string have indices that run from 0 to string.length() - 1. The method charAt() returns the character at a specified index.
char ch = firstName.charAt(2); // ch = 'a'

Concatenation of Strings
String fullName = firstName + lastName;
String name = firstName.concat(lastName);

Substring
String welcome = new String ( "Hello World" );
String hello = welcome.substring (0, 5);
String world = welcome.substring (6);

Comparisons of Strings
The boolean operator ( == ) returns true only if the two strings refer to the same String object. Use method equals() or equalsIgnoreCase() to test or equality.

String str1 = "abcd";
String str2 = "abcd";
String str3 = new String ( "abcd" );
String str4 = new String ( "ABCD" );
( str1 == str2 ) => true
( str1 == str3 ) => false
( str1.equals(str3) ) => true
( str2.equalsIgnoreCase(str4) ) => true
The method compareTo( String anotherString ) compares two Strings lexicographically. It returns 0 if the two strings are the same. It returns a negative number if this String precedes anotherString and returns a positive number if this String follows anotherString.
int x = str2.compareTo(str4); // x > 0

String Conversions
To convert all the characters of a string to lower case use the method toLowerCase(). To convert all the characters of a string to upper case use the method toUpperCase().

String major = "Computer Science";
String majorLower = major.toLowerCase();
String majorUpper = major.toUpperCase();
The method trim() returns a string with the leading and trailing whitespace omitted. The method replace ( char oldChar, char newChar) will return a new string by replacing all occuurences of the oldChar in this string with newChar.

Finding Characters