String and StringBuffer

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);

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 for 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 occurrences of the oldChar in this string with newChar.

Finding Characters

Finding Substrings

Converting Character Arrays to Strings and vice versa

char[] charArray = { 'H', 'e', 'l', 'l', 'o' };
String hello = new String ( charArray );

String world = new String ( "World" );
char[] worldArray = world.toCharArray();

Converting Numeric values to Strings

double x = 2.512;
String xStr = String.valueOf ( x );

Test Prefix and Suffix of Strings

String str = "polymorphism";
str.startsWith ("poly") => true
str.endsWith ("ism") => true

Splitting a String

String line = "This is a test";
String[] result = line.split ( "\\s" );
for ( int i = 0; i < result.length; i++ )
  System.out.println ( result[i] );

StringBuffer Class

A String object is immutable. A StringBuffer object is like a String object but can be modified. A string buffer is a sequence of characters but the length and content of the sequence can be changed through certain method calls.

The principal operations on a StringBuffer are append() and insert() methods. The append() method adds characters at the end of the buffer and the insert() method adds the characters at a specified location.

Every string buffer has a capacity. As long as the length of the character sequence does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If there is an overflow, the string buffer is automatically made larger.

Creating a StringBuffer Object

StringBuffer strBuf1 = new StringBuffer();
StringBuffer strBuf2 = new StringBuffer ( 100 );
StringBuffer strBuf3 = new StringBuffer ( "Hello World" );

Getting the capacity of a String Buffer

int x = strBuf2.capacity();

Adding Characters or Strings to a String Buffer

strBuf2.append ("put");
strBuf2.insert (0, "out");

Delete or Replace Characters in a String Buffer

strBuf2.setCharAt (6, 'p');

Reverse the Character Sequence

StringBuffer strBuf4 = strBuf2.reverse();

Get length of the character sequence in a String Buffer

int x = strBuf4.length();

Convert a String Buffer to a String

String str = strBuf4.toString();

Command Line Arguments

You can pass parameters to the method main() from the command line. The command line parameters are stored in the String array args. Supposing you start the program as follows:
java prog 1 2 3
The numbers 1, 2, and 3 will be stored as strings "1", "2" and "3" in the array args. For example, the string "1" will be in args[0] and the string "3" will be in args[2].