Exercise on Strings and Methods

You may wish to refer to the sidebar on Selected String Methods.

Part 1: For each of the following expressions, what does it produce?

  1. "Hello"

  2. "Hello" + "There"

  3. "     Where areyou   today         ".trim()

  4. "Where are you today".trim()

  5. "aaaaAAAA" + "BBBBbbbb"

  6. "a string".toUpperCase()

  7. "Foo on".toUpperCase() + " hello"

  8. "BAM BAM BAM " + "BOOM".toLowerCase()

  9. "This is really long".length()

  10. "Some would say this is short".length()

  11. "Friends".toUpperCase() + "this has twenty eight letters".length()

  12. "Big".toUpperCase().toLowerCase()

  13. "What is the substring?".substring(2, 6)

  14. "How many letters?".substring(10)

  15. "This is a tricky one".toUpperCase().substring(5, 10).length()

  16. "I Love Lucy".lastIndexOf('L')

  17. "Animal farm".substring("George Orwell".lastIndexOf('o'))

Part 2: For each of the following, write an expression that converts the input String(s) to the output String, subject to:

  1. Input: "This is a string"
    Output: "THIS IS A STRING"   Follow this link to check your answer.

  2. Inputs: "Hello there" and " Sailor"
    Output: "Hello there Sailor"

  3. Inputs: "Do you know?" and "What "
    Output: "What do you know?"

  4. Inputs: " What is " and " your NAME?"
    Output: "What isyour name?"   Follow this link to check your answer.

  5. Inputs: "This one is " and " IS tricky"
    Output: "This one IS tricky"

  6. Input: "The last"
    Output: "LAST"

  7. Inputs: "FOO BAR CITY" and "This city is BIG"
    Output: "foo bar CITY"

  8. Inputs: "Bilbo" and "Gandalf"
    Output: "bil**G*nd*lf"

  9. Inputs: "save the dolphins" and "we love them"
    Output: "THE DOLPHINS love"