6 - More String Methods

Get an introduction to or review of advanced string methods in Java programming.

Code HuntJava
1.0x

6 - More String Methods

Created 3 years ago

Duration 0:07:03
lesson view count 16
Get an introduction to or review of advanced string methods in Java programming.
Select the file type you wish to download
Slide Content
Tags: Code Hunt Java
  1. More String methods

    Slide 2 - More String methods

  2. Searching within Strings

    Slide 3 - Searching within Strings

    • To determine if a particular substring appears in a String, use the contains() method.
    • The contains() method returns a boolean value:
    • "hamburger".contains("burger")
    • "birthday".contains("birch")
    • "great eight".contains("ate")
    • As with most String operations, contains() is case-sensitive:
    • "Apple Pie".contains("pie")
    • "Apple Pie".contains("Pie")
  3. Searching within Strings

    Slide 4 - Searching within Strings

    • The beginning or end of a String can be compared to another String using startsWith() and endsWith(), which also return booleans.
    • startsWith() and endsWith() are also case-sensitive:
    • "happy birthday".startsWith("happy")
    • "happy birthday".endsWith("day")
    • "happy birthday".startsWith("birth")
    • "happy birthday".endsWith("birth")
    • "Happy birthday!".endsWith("day")
    • "Happy birthday!".startsWith("happy")
  4. Searching within Strings

    Slide 5 - Searching within Strings

    • To find where in a String a particular substring occurs, use the indexOf() method.
    • indexOf() returns the first index at which the substring occurs, or -1 if it does not occur at all:
    • "apple pie".indexOf("apple")
    • "apple pie".indexOf("pie")
    • "apple pie".indexOf("cake")
    • "apple pie".indexOf("Pie")
    • An optional second argument to indexOf() indicates at what index to being searching:
    • "banana split".indexOf("na", 0)
    • "banana split".indexOf("na", 3)
    • "banana split".indexOf("na", 6)
  5. Retrieving characters

    Slide 6 - Retrieving characters

    • Each individual char can be extracted from a String using the charAt() method:
    • "abcdefg".charAt(3)
    • "sandwich".charAt(0)
    • "pineapple".charAt(8)
    • "pineapple".charAt(9)
    • String str;
    • ...
    • str.charAt(str.length())
  6. Check Your Understanding

    Slide 7 - Check Your Understanding

  7. Check Your Understanding

    Slide 8 - Check Your Understanding

  8. Check Your Understanding

    Slide 9 - Check Your Understanding

  9. Check Your Understanding

    Slide 10 - Check Your Understanding