Strings


 * __Strings__** - class that represents character strings. Strings are an ordered sequence of characters. They are for visual purposes only, displaying text such as, "Meermans08". Strings are counted both as a primitive type and a class unto itself.

When creating a string, declare a variable to be a referance to an object. For example,

code format="java" String sentence; code The variable //sentence// is declared as a reference to a String object. Afterwards, create (a.k.a. instantiate) it using the //new// operator. For example,

code format="java" String sentence = new String("Computer Science is fun!"); OR String sentence = "Computer Science is fun!";  <simple

code After the string is created, you can do many things with it using different methods. For example, code format="java" int sentenceLength = sentence.length; //returns the length of the string and sets it into the integer sentenceLength

sentence = sentence.toUpperCase; //makes the string all upper case String low = sentence.toLowerCase; //makes another string where the initial string is all lower case

String valentine = new String("Valentine's Day "); String not = new String("is not a real holiday."); String together = valentine.concat(not); System.out.print(together); //concatenates (adds) the string not at the end of the string valentine //prints Valentine's Day is not a real holiday.

String Creation(2 ways):examples - String annoying Sound = newString("Pika-chu"); - String annoying Sound = "Pika-chu";

code


 * __String Methods__**

__(int) **.compareTo** (some string to compare against)__ //**returns:**// a negative number if 1st is alphabetically before 2nd, a 0 if 1st and 2nd are equal, and a positive if 1st is alphabetically after 2nd. It's good for sorting names or things from one another. Although you cannot say if one is less than or greater than another in an if-statement (if "pika-chu" < "raichu"), you can say if one equals one another in an if-statement (if "pika-chu" == "raichu"). //**examples:**// If **String rawr = "dinosaur";** is your String, then answer the following ... 1. **rawr.compareTo ("koala");** //Since "dinosaur" comes before "koala" in the alphabet then you would return a negative number, which is most likely -1.// 2. **rawr.compareTo ("aardvark");** //Since "dinosaur" comes after "aardvark" in the alphabet then you would return a positive number, which is most likely 1.// 3**. rawr.compareTo("dinosaur"); answer:** 0 //Since "dinosaur" is equal to "dinosaur" in the string, then you would return a 0.//
 * //explanation://**
 * answer:** -1
 * answer:** 1

__(boolean) **.equals** (some string to compare against)__ TRUE if 2 strings are EQUAL, returns FALSE if 2 strings are UNEQUAL //**explanation:**// Compares to see if two strings equal one another, almost like an alternative for if(myVariable.equals("pika-chu")). It DOES matter though if there are capital and lowercase letters involved. If **String rawr = "dinosaur";** is your String, then answer the following ... 1. **rawr.equals ("koala");** //Since "koala" does not equal "dinosaur", then you would return false.// 2. **rawr.equals ("dinosaur");** //Since "dinosaur" equals "dinosaur", then you would return true.//
 * //returns://**
 * //examples://**
 * answer:** false
 * answer:** true

__(int) **.indexOf** (some string to search for)__ //**returns:**// the index(position) of the beginning of the string searched for; returns -1 if not found //**explanation:**// For **String rawr = "dinosaur";** the method .indexOf could ask **rawr.indexOf("dino");** which would be asking if "dino" is in your word "dinosaur". Since it found "dino" in "dinosaur", then it will return the slot number of the beginning of the string "dino", which would be 0. But if, for example, you were trying to find "dino-saur" or even "dino saur" then the method .indexOf would return a -1 because it was unable to locate the ENTIRE string you asked for. In other situations where certain phrases are repeated in the string such as the "iss" in "Mississippi", if you are asking for **stateName.indexOf("iss")**, then it will only return the slot number of the first "iss" found, which would be 1. If **String rawr = "dinosaur";** is your String, then answer the following ... 1. **rawr.indexOf ("koala");** //Since "koala" is not found in "dinosaur", then you would return a -1.// 2. **rawr.indexOf ("saur");** //Since "dinosaur" was found in "dinosaur", then you would return the beginning of the string "saur", which is 4.//
 * //examples://**
 * answer:** -1
 * answer:** 4

__(int) **.length**__ //**returns:**// the number if characters in the string If you had **String rawr = "dinosaur";** then the method .length could ask **rawr.length** which would return 8 because there are 8 slot numbers in the string "dinosaur". Remember the parenthesis at the end, there was an error that said that the .length method does not require parenthesis, this is false. If **String rawr = "bubbles";** is your String, then answer the following ... 1. **rawr.length ;** //Since there are 7 characters in the string, you would return 7.//
 * //explanation://**
 * //examples://**
 * answer:** 7

__(string) **.substring** (start index)__ //**returns**//: a string starting at index and going to the end //**explanation:**// If you had **String rawr = "dinosaur";** then the method .substring could ask **rawr.substring(4)**. The method would then go to the 4th slot and chop off the rest of the string, returning **"saur"**. If **String rawr = "dinosaur";** is your String, then answer the following ... 1. **rawr.substring (2);** //Since the 2nd slot in "dinosaur" is n, everything after slot 2 would be cut, leaving you with "nosaur".//
 * //examples://**
 * answer:** nosaur

__(string) **.substring** (start index, end index)__ //**returns:**// a string starting at 1st number and ending at 2nd number If you had **String rawr = "dinosaur";** then the method .substring could ask **rawr.substring(1, 3);**. The method would then go to the starting point which is 1 and cut off the "d" leaving you "inosaur". Then the method would go to the ending point which is 3 and chop off the rest of the string INCLUDING the ending point which is "osaur". The method would return "in". In other situations, the method .substring could ask **rawr.substring(3).substring(2);**. The method would go to the 3rd slot, and chop off the string leaving you with "osaur" as the new string. Then the method would go to the 2nd slot of "osaur" and chop off the rest of the string, leaving you with "aur". If **String rawr = "dinosaur";** is your String, then answer the following ... 1. **rawr.substring (3,6);** //Everything starting from slot 3 and BEFORE slot 6 would be cut, so slots 3, 4, and 5 would be cut off, leaving you with osa.//
 * //explanation://**
 * //examples://**
 * answer:** osa

code ex. String x = "carcassonne";

int a = x.length;                   //a = 11 int b = x.indexOf("son");             //b = 6 int c = x.compareTo("blokus");        //c = +1 string d = x.substring(4,8);          //d = asso boolean e = x.equals("carcassonne");  //e = true

code