CS173: Intro to Computer Science - Strings
Activity Goals
The goals of this activity are:- To be able to explain that a
String
is a class that stores text and provides functionality that manipulates that text. - To be able to explain that a
String
is immutable, and that new memory is allocated for aString
when it is modified. - To be able to create and manipulate a
String
. - To be able to invoke the
concat()
,indexOf()
,substring()
andreplace()
methods of aString
.
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.Warmup
Trace this algorithm to swap twoint
variables.
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: The String
1 2 3 4 5 6 7 8 9 10 11 | public class Main { public static void main(String[] args) { // These are equivalent // String x = "Test"; String x = new String( "Test" ); // So are these: x = "Another test" ; x = new String( "Another test" ); } } |
Questions
- Unlike re-assigning a primitive variable, assigning a variable to an object creates a new object. A
String
is a class that allocates memory for and stores text. Since String text cannot be reassigned once it is allocated (and must be re-created instead), aString
is called an immutable object. Why can’t aString
be re-assigned like anint
ordouble
can?
Model 2: String
Manipulation with substring

Questions
- What is the difference between
x = x.concat(y)
andx = x + y
forString
objectsx
andy
? - Rewrite the statement
String z = "Cheese" + "Hamburger".substring(3);
using theconcat
function.
Embedded Code Environment
You can try out some code examples in this embedded development environment! To share this with someone else, first have one member of your group make a small change to the file, then click "Open in Repl.it". Log into your Repl.it account (or create one if needed), and click the "Share" button at the top right. Note that some embedded Repl.it projects have multiple source files; you can see those by clicking the file icon on the left navigation bar of the embedded code frame. Share the link that opens up with your group members. Remember only to do this for partner/group activities!Model 3: String
Searching with indexOf

Questions
- What is the difference between the two
indexOf()
methods given above? How do you know which version you are calling from a program?
Model 4: More String
Functions

Questions
- What is the difference between the two
replace()
methods given above? How do you know which version you are calling from a program? - Write a Java statement to replace "Ham" with "Cheese" in a
String
"Hamburger"
Model 5: Working with String
s
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class Main { public static void main(String[] args) { String x = "Test" ; String y = new String( "Test" ); if (x.equals(y)) { System.out.println( "x and y contain the same text!" ); } String z = "tan" ; System.out.println(z.substring(1, 2) + z.substring(0, 1)); } } |
Questions
- Why is it necessary to write
x.equals(y)
instead ofx == y
to check if twoString
objects contain the same text? - What does the print statement output for the string
z
? - How might you print only the "est" characters from
x
using thesubstring
method? - You can compare two strings using
str1.equals(str2)
. Why do you thinkstr1 == str2
won't work? - Java includes a
equalsIgnoreCase
function. How do you think this works, using other functions that theString
class provides? - Write a function that accepts two
String
parameters and returns true if the third character of eachString
is the same. Be careful to check the length of eachString
first! - Using
String
methods, create a variable containing the word "Book" and the word "keeper", and create the word "Bookkeeper" and "Beekeeper". There are multiple ways to do this! - Read a String from the console, check that it is exactly a 5-letter word, and if so, convert it to upper-case. Replace all the vowels with asterisks Print the result!
- Read a String from the console containing only the letters A, C, G, and/or T, and convert it to uppercase. Replace the A's to T's, and all the T's to A's. Replace the G's with C's and all the C's to G's.
- Challenge: there is a String function called matches that will check if a text pattern exists inside the String. It returns a boolean (true if the pattern is found, false if not). Use the matches function to see if any letter besides A, C, G, or T is in the String. Here are some hints:
.*
means to search for any character (the.
represents this) any number of times (the*
). You can check for a particular character using brackets like this:[ACGT]
will match one character that is equal to A, C, G, or T. Try searching for any number of characters, followed by a character in the alphabet other than A, C, G, or T, followed by any number of characters.