1 / 27

Strings part 1 of 2

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”

taylor
Download Presentation

Strings part 1 of 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings part 1 of 2 Strings What you already know about strings Stings as a class Gory Details Applications

  2. 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”

  3. 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“);

  4. 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();

  5. 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!

  6. 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

  7. 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!

  8. 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?

  9. 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) _________

  10. 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

  11. 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

  12. Strings as a class fullName.toLowerCase() Always the same method name The ‘dot’ EVERY String type has this method available!

  13. 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 ());

  14. TEST Write the code to display The City is SAN FRANCISCO cityName = “San Francisco”; Hint: look at the previous slide! System……

  15. Strings Gory Details Actually JAVA and other languages, store a string like this: Every character gets it’s own location

  16. 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!

  17. 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

  18. 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); } }

  19. String Demo -- The Result Text processing is hard! 012345678901234567890123 The word "hard" starts at index 19 The changed string is: Text processing is easy!

  20. 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");

  21. 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!!

  22. 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);

  23. 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. */

  24. 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)

  25. 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!

  26. 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?

  27. REVIEW • What position is the comma? • How many characters are there? • What does substring (8, 12) Return?

More Related