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?
-
"Hello"
-
"Hello" + "There"
-
"     Where areyou   today         ".trim()
-
"Where are you today".trim()
-
"aaaaAAAA" + "BBBBbbbb"
-
"a string".toUpperCase()
-
"Foo on".toUpperCase() + " hello"
-
"BAM BAM BAM " + "BOOM".toLowerCase()
-
"This is really long".length()
-
"Some would say this is short".length()
-
"Friends".toUpperCase() + "this has twenty eight letters".length()
-
"Big".toUpperCase().toLowerCase()
-
"What is the substring?".substring(2, 6)
-
"How many letters?".substring(10)
-
"This is a tricky one".toUpperCase().substring(5, 10).length()
-
"I Love Lucy".lastIndexOf('L')
- Do you see how the notation for characters differs from that of strings?
-
"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:
- You can apply any String methods you want
(including concatenation) to the input Strings.
- You are not allowed to write an input String more than once.
So, for example, if "Hello" is the input,
you are not allowed to write answers like
"Hello".toLowerCase() + "Hello".toUpperCase().
- When there is more than one input, you can use the inputs in any order you choose.
- Input:
"This is a string"
Output: "THIS IS A STRING"
  Follow this link to check your answer.
- Inputs:
"Hello there" and " Sailor"
Output: "Hello there Sailor"
- Inputs:
"Do you know?" and "What "
Output: "What do you know?"
- Inputs:
" What is " and " your NAME?"
Output: "What isyour name?"
  Follow this link to check your answer.
- Inputs:
"This one is " and " IS tricky"
Output: "This one IS tricky"
- Input:
"The last"
Output: "LAST"
- Inputs:
"FOO BAR CITY" and "This city is BIG"
Output: "foo bar CITY"
- Inputs:
"Bilbo" and "Gandalf"
Output: "bil**G*nd*lf"
- Inputs:
"save the dolphins" and "we love them"
Output: "THE DOLPHINS love"