270 likes | 448 Views
Strings part 1 of 2. Strings What you already know about strings Stings as a class Gory Details Applications. Strings-- What you already know. The simple definition Strings are a sequence of characters: 1) “ABC123” 2) “My dog ate my homework” 3) “123”
E N D
Strings part 1 of 2 Strings What you already know about strings Stings as a class Gory Details Applications
Strings-- What you already know The simple definition Strings are a sequence of characters: 1) “ABC123” 2) “My dog ate my homework” 3) “123” 4) “$$$ Contact your System Administrator $$$” They include spaces, numbers, special characters, embedded carriage returns & tabs. They are not a primitive data type. 123 is not the same as “123”
Strings-- What you already know You have already seen how to display a string: System.out.println (“Hello World"); You have already seen how to display a string and a number: System.out.println (“Total cost is“+ totalCost); You have already seen how to embed escape characters: System.out.println(“\n\n\t Enter a number“);
Strings-- What you already know You have already seen how to declare and get a string: String fName;// The users first name // ---------Get Input from user------------- // first get ready to accept keyboard input Scanner keyboard = new Scanner(System.in); // Get the users name System.out.print("\n\n\tWhat is your first Name? "); fName = keyboard.next();
Strings-- What you already know Look at declaring a String: String fName;// The users first name String is the type fName is the variable Note: variables of type String, hold strings!
Strings-- What you already know Nothing strange about variables as Strings Examples String reason1 = “My dog ate my homework”; String reason2 = “I had to sort my socks”; String fName; String mInitial; String lName; Some fixed message As a variable to be defined later
Another look at concatenation Suppose fName = “Bob”; mInitial= “E”; lName=“Armstrong”; System.out.println (fname + ” “ + minitial+ “ “ +”.”+ lName); Prints out Bob E. Armstrong Note the use of spaces!
Another look at concatenation Suppose (names are defined as before) String fullName; fullName = fName; fullName = fullName + “ “ + mInitial+ “.”; fullName = fullName + “ “ +lName; System.out.println (fullName); Prints out Bob E. Armstrong What do you like better?
Strings as a class Remember Class? Class values are objects Objects have methods What sort of methods would you expect Strings to have? (make a guess!) a) _________ b) _________ c) _________
Strings as a class Generally speaking, a String should have methods to: Change to upper/lower case Search for a substring Give the length of the string Compare two strings
Strings as a class Here is how you work with methods for String (same basic principal for ALL methods!!!!) Assume fullName is of type String The method for changing to lower case is: toLowerCase() You use it like this: fullName.toLowerCase() You find the methods in the text
Strings as a class fullName.toLowerCase() Always the same method name The ‘dot’ EVERY String type has this method available!
String – Methods To change a string to upper case or lower case: System.out.println("Your full name in lower case is " + fullName.toLowerCase ()); System.out.println ("Your full name in upper case is " + fullName.toUpperCase ());
TEST Write the code to display The City is SAN FRANCISCO cityName = “San Francisco”; Hint: look at the previous slide! System……
Strings Gory Details Actually JAVA and other languages, store a string like this: Every character gets it’s own location
Strings Gory Details How many characters are in the string? Answer: 7 Why: The locations start counting at ZERO! Doupt! Blanks are counted as characters ! Is a character I have to repeat this: The locations start counting at ZERO! Doupt!
String Demo -- The code This demo will display a sentence, locate a given word, then change the word to another word and Don’t change any other words in the sentence
String Demo -- The code public class StringDemo { public static void main(String[] args) { String sentence = "Text processing is hard!"; int position; position = sentence.indexOf("hard"); System.out.println(sentence); System.out.println("012345678901234567890123"); System.out.println("The word \"hard\" starts at index " + position); sentence = sentence.substring(0, position) + "easy!"; System.out.println("The changed string is:"); System.out.println(sentence); } }
String Demo -- The Result Text processing is hard! 012345678901234567890123 The word "hard" starts at index 19 The changed string is: Text processing is easy!
String Demo -- The Breakdown //Starts with a 24 length string String sentence = "Text processing is hard!"; //declares an variable (integer) called position int position; /* Uses a predefined method called indexOf(A_String) to locate the position the word “hard” */ position = sentence.indexOf("hard");
String Demo -- The Breakdown /* Displays the original sentence and their position as the computer stores them */ System.out.println(sentence); System.out.println("012345678901234567890123"); //This is a great idea to test your work!!
String Demo -- The Breakdown /*The only thing new is the backslash before the double quotes! */ //That’s the only way to display quotes! System.out.println("The word \"hard\" starts at index "+ position);
String Demo -- The Breakdown /*This is the hard part. substring (start, end) returns the portion of the string in between start and end */ sentence = sentence.substring(0, position) + "easy!"; /*Then the + concatenates the word “easy!” Lucky the word hard was the last word in the string, otherwise it would have been harder. */
String Demo – The LOGIC Step 1: Locate the position of the given word Step 2: Make a temp sentence of all words BEFORE the given word Step 3 Make a temp sentence of all the words AFTER the given word Step 4: Replace the given word by using concatenation New Sentence = (All words before) + “new-word” + (All words after)
String Demo – The LOGIC Notes: 1) The given word was the LAST word, so we didn’t need To make a temp sentence of all the words AFTER the given word 2) Eventually, doing the LOGIC first will make programming easier!
REVIEW • Are Strings a primitive data type? • Write the code to display You are age years old • age is an integer • Given the String: • Hi Mom, Happy Birthday! • How many characters are there?
REVIEW • What position is the comma? • How many characters are there? • What does substring (8, 12) Return?