Lesson+03+-+Simple+String+Operations

=Lesson 3=

Adrian, Felix, and James
=**Concatenation Operator**- the use of the plus sign, + to add strings together = Ex:

String mm= “Hello”; String nx = “good buddy”; String c = mm + nx; //Concatenation// //System.out.println(c); //prints Hellogood buddy…where there is no space between the o in Hello and the g in good.

= = =**length** - The length method is used to find the number of characters in a String. // Start counting with 1 for length!!!! //= Ex: System.out.println("Benito Juarez".length); //13//

//**substring(x) - Get part of string starting at x and going to the end of the string.**// //Ex: // //String myPet = “Sparky the dog”; // //String smallPart = myPet.substring(4); // //System.out.println(smallPart); //prints ky the dog

This is because various characters in a String are numbered from the left beginning with zero. These numbers are called indices, which count spaces as well. S p a r k y _ t h e _ d o g has the index of 4 therefore anything from k to the end 0 1 2 3 4 5 6 7 8 9 10 11 12 13 prints. In this case its “ky the dog”.
 * S || p || a || r || k || y ||  || t || h || e ||   || d || o || g ||
 * 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 ||

System.out.println(myPet.substring(2,5)); //prints out ark// //System.out.println(myPet.substring(11,14)):// prints out dog, but probably better to use myPet.substring(11);
 * substring(x, y)** - Get part of string starting at index x and ending at index y-1; The second argument has to be the index number one more than what you want.

=**<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Conversions to lowercase **= <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">**toLowerCase** converts all characters to lower case (small letters)

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Ex: <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">String bismark = “Dude, where’s MY car?” <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(bismark.toLowerCase ); //prints dude, where’s my car?//

=//<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">toUpperCase converts all characters to upper case ( capital letters) //= //<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Ex: // //<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(“Dude, where’s MY car?”.toUpperCase ); // prints DUDE, WHERE’S MY CAR?

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">It’s possible to concatenate a String with a numeric variable
 * <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Concatenating a String and a numeric: **

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">int x = 27; <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">String s = “What have we done?”; <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">String combo = s+ “ “ + x; <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(combo); //prints What have we done? 27//

=**//<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Escape Sequences -- //**//<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">The escape character is the forward slash /. A combination of a forward slash and the following character is the escape sequence. A forward slash tells java that it must examine the next character after the slash to see what to do. //= <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">\" -- tells java that this quote is just another character in the current string; it is NOT signaling the end of the string. \\ -- tells java that this slash is just another character in the current string; it is NOT another escape character. <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">\n -- tells java to put a newline (line break); when printed, the string contains neither the / nor the n. <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">\t -- tells java to jump to the next tab; when printed, the string contains neither the / nor the t.

When counting, the length of an escape sequence (the slash and the character after) is only 1.

Example of quote being interpreted as just another character of the String, not the ending quote of a String.: <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">String s = “What \”is\” the right way?”; <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(s); prints What “is” the right way?

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">This is what makes “is” quoted

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">**To create a new line** which is called line break is done as follows

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">String s “Here is one line\nand here is another.”; //note there is no space between the \n and the a in and. <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(s);

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Prints the following: <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Here is one line <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">and here is another

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">**Escape sequence \\,** will allow to print a backslash within String. If a single <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">\ is inserted it will be interpreted as the beginning of an escape sequence.

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(“Path = c:\\nerd_file.doc”);

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Prints

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Path = c:\nerd_file.doc

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">**The escape sequence \t,** will allow to “tab” over. This is much more convenient than spacing the Strings. In this example the code tabs twice

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">System.out.println(“Name:\t\t Address:”)

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Prints

<span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Name: ** <span style="font-family: 'Times New Roman','serif'; font-size: 12pt; margin: 0in 0in 0pt;">Address: