Strings A Review
Strings A Review
Almost all String problems involve using the index of the letter in the word to do things. Let's
review everything we need to know:
When we work with Strings in Java, we need to know how to do the following skills:
Use methods from the String class to perform actions: String (Java Platform SE 7 )
Create a new String using pieces of the old String
Count or check if a String contains something
String Basics
Strings are a sequence of characters. Each letter is assigned a position known as an index.
char
H E L L O 1 1 s
0 1 2 3 4 5 6 7 8
index
A char is a different data type than String. The char is the individual letter in the String. In Java,
we interact with char and String very differently.
char String
Example
Comparing
String Methods
Understanding how to read and use documentation is the most important skill any programmer
can have.
When we call String methods, we have to perform an action on the String. So we say
String.methodName(parameters)
The method is the action being performed, and str is the String that is performing the
action. Using the example above, we would call the isEmpty() method to check the String
called str using the code below:
str.isEmpty()
isEmpty() returns a boolean value (true or false) as the return type. To use it in a program, we
would probably write something like this:
if (name.isEmpty()){
System.out.println("Error: Please enter a name.");
}else{ …
Common String Methods
There are a lot of String methods so I will write the ones you will use most often below. There
are many other helpful methods in the documentation though.
String .substring(int start) String starting from start to String ans = str.substring(2);
the end
String .substring(int start, int stop) String from start to stop, String ans = str.substring(2, 5);
exclusive
int .indexOf(String str, int index) Find index of str in the int i = str.indexOf("a", 10);
String. Start looking at
index. -1 if not found
int .lastIndexOf(String str) Find the last index that str int i = str.lastIndexOf("i");
is located at
Most problems involve using a for loop to generate the index so we can examine each letter of
the String and perform some action.
int count = 0;
if (letter == 'a'){
count++;
}
}
//Return true if there are two identical letters next to each other
if (str.charAt(i) == str.charAt(i+1)){
return true;
}
}
if (str.charAt(i) != 'z'){
ans += str.charAt(i);
}
}
Practice Questions
1. Given two strings, append them together (known as "concatenation") and print the result.
However, if the concatenation creates a double-char, then omit one of the chars, so
"abc" and "cat" yields "abcat" since there would have been two c's next to each other.
3. Count the number of times the word 'jo' appears in a String. Print true if the number of
times "jo" appears in the String is even.
countJo("jojo") → true
countJo("jo123") → false
countJo("jojojo") → false
4. Write a method that will return a new String with only the first and last two letters of the
original String. If the String is 4 letters or smaller, return the original String unchanged.
changeString(“apple”) → “aple”
changeString(“joy”) → “joy”
5. Given a String, return a new String containing only the vowels.
onlyVowels("abcde") → "ae"
6. Given a String, without the .replace() method, print a new String changing all the "z" to
"s".
changeZ("zebra") → "sebra"