Lesson+03


 * Lesson #03: Simple String Operations - Rachel**


 * Concatenation:**
 * First and foremost is concatenation. We use the plus sign, +, to do this

code //String theName = “Donald Duck”; int len = theName.length; System.out.println(len);
 * Use the length method to find the number of characters in a String:

//prints 11…notice the space gets counted// code

We can pick out a piece of a String…substring code format="text" String myPet = “Sparky the dog”; String smallPart = myPet.substring(4); System.out.println(smallPart);
 * A piece of a String (substring):

//prints ky the dog code

The various characters in a String are numbered starting on the left with 0. These numbers are called indices. (Notice the spaces are numbered too.) S p a r k y t h e d o g … so now we see that the ‘k’ has index 4 and we go from 0 1 2 3 4 5 6 7 8 9 10 11 12 13 k all the way to the end of the string to get “ky the dog”. A more useful form of substring: code format="text" String myPet = “Sparky the dog”; String smallPart = myPet.substring(4, 12); System.out.println(smallPart);

//prints ky the d code

code format="text" String bismark = “Dude, where’s MY car?”; System.out.println( bismark.toLowerCase );
 * Conversion between lower and upper case:**
 * toLowerCase converts all characters to lower case (small letters)

// prints dude, where’s my car? code

code format="text" System.out.println( “Dude, where’s My car?”.toUpperCase );
 * toUpperCase converts all characters to upper case (capital letters)

//prints DUDE, WHERE’S MY CAR? code

code format="text" int x = 27; String s = “Was haben wir gemacht?”;// German for “What have we done?” String combo = s + “ ” + x; System.out.println(combo);
 * Concatenating a String and a numeric:

//prints Was haben wir gemacht? 27 code

code format="text" String s = “What **\"is\"** the right way?”; System.out.println(s);
 * Escape sequences:**
 * What "is" the right way?

// prints What **"is"** the right way? code

code format="text" String s = “Here is one line**\n**and here is another.”; System.out.println(s);

//Here is one line and here is another. code

code format="text" System.out.println(“Path = c**:\\**nerd_file.doc”);

//Path = c:**\**nerd_file.doc code

code format="text" System.out.println(“Name:\t\tAddress:”);
 * The escape sequence, \t, will allow us to “tab” over. The following code tabs twice.

//Name:   Address: code