1 / 12

Strings Are Important

Strings Are Important. The String class is one of the most important supplied classes with Java. Besides int and doubles we use chars (single characters) and Strings (multiple characters) the most. Strings are an abstract data type in Java (cf. primitive data types). java.lang.

Download Presentation

Strings Are Important

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 Are Important • The String class is one of the most important supplied classes with Java. • Besides int and doubles we use chars (single characters) and Strings (multiple characters) the most. • Strings are an abstract data type in Java (cf. primitive data types)

  2. java.lang • They are part of the java.lang package. • A String is a sequence of characters. • Because it’s a sequence, it has an order… • that is, a beginning, a middle, and an end.

  3. String sequences • Each character in a string can be identified by its sequential position. • This position is a number called the index. • The 1st character in a string has an index of zero (0). • This is weird but universal in programming languages.

  4. My Favourite String • Imagine the string “billy”. • Note that billy has 5 characters but that the highest index is 4. • That’s because the index begins at 0. • The index of the last character is the length -1. • This is a big “gotcha”.

  5. Spaces Are Characters Too • The string “billy billy billy.” is 18 characters long. • Why? • Because the spaces count as characters. • So does the period.

  6. String Methods • Java supplies numerous methods to deal with strings. • See p138 in your text. • Lets focus on a couple of key String methods. • Assume: String phrase = “billy”; • length() • System.out.println(phrase.length()); prints 5.

  7. String Methods • toLowerCase() returns a copy of the string with all lower case letters. • System.out.println (phrase.toLowerCase()); prints billy. • toUpperCase() returns a copy of the string with all upper case letters. • System.out.println (phrase.toUpperCase()); prints BILLY.

  8. String Methods • replaceFirst(String str, String str2) returns a string with the 1st occurrence of str replaced by str2. • System.out.println (phrase.replaceFirst(“ill”, “obb”)); prints bobby. • str and str2 do not have to be the same length. • cf. replaceAll(String str, String str2)

  9. String Methods • You need to know all the methods on P138 for the test. • N.B. when we say that a method returns something we mean that the thing which is returned completely replaces the program code that was there before. • So if you assign the result of a method to a variable it must have the right data type.

  10. You Do. • P139: Review: AccountSetup

  11. String Methods (comparison) • Some of the most important methods have to do with comparing strings. • We cannot compare strings using the standard relational operators (<, <=, ==, >, >=). • We must use special String comparison methods instead. • See P140 for a list of important String comparison methods. • Let’s go over the list together.

  12. You Do • P141: Review: FormalGreeting

More Related