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] );
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
strBuf3.delete(6,11); strBuf2.replace(3,6,"look"); 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 3The 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].