The Coder's Handbook
Strings
USING STRINGS
What is a String?
A String is a type of variable that is used to represent a sequence of characters. It’s not one of the eight primitive types; it is an object. You can declare, initialize, and assign strings in the same way as you would work with other variables.
Behind the scenes, a String is just a fancy way of storing an array of characters. If you really needed, you could make your own String class. But since storing text is so common in program, Java created this built-in object for our convenience.
Comparing Strings
When comparing Strings, you will almost always want to use the .equals() or .equalsIgnoreCase() methods. These methods check the contents of two Strings and check if they are the same. For example
if(input.equals("waffle"))
{
System.out.println("Waffles are far superior to pancakes. Good decision.");
}
You want to avoid using the == operator with Strings. What this does is actually compare the memory addresses of two Strings. This is rarely the behavior you're looking for.
String Index
You can access a specific element in a String using methods like charAt and substring, which are shown below. It's important to note that with Strings (and arrays) we start counting at zero. For example, in the String "Hello" the index postion of the 'e' is 1.
Immutability
Aside from using the assignment operator, you can't change a String. You'll see a lot of methods in the list below that sound like they change a String, but actually return a copy of the String with those changes. Because of this trait, we call Strings immutable. It simply means they're unchanging (can't be mutated).
Core String Methods
These are the most common String methods and are part of the AP Subset. You'll need to know them for your exam, and will find that they come up often when working with text.
int length() - Returns the length of the String as an integer
for(int i = 0; i < word.length(); i++)
char charAt(int index) - Returns the character stored a the specified index
int letter = word.charAt(i);
String substring(int start, int end) - Returns a String containing everything from start to end, excluding end
String word = "where"
String smallerWord = word.substring(1, 4); // smallerWord is her
String substring(int start) - Returns a String containing everything from start to the end of the String
String word = "applepie"
String smallerWord = word.substring(5); // smallerWord is pie
String toUpperCase() - Returns an uppercase copy of the String. This does not change the original string.
String shouting = word.toUpperCase();
String toLowerCase() - Returns an lowercase copy of the String. This does not change the original string.
String quiet = word.toLowerCase();
This is witty, yet... I am pretty sure programmers made Pokemon. Get it together Jen.
Advanced String Methods
These are less commonly used but still very handy tools to have. You won't be tested on these during an exam during class, but may be encouraged to use them during projects. In short: know these are here, but it's okay if you don't have the syntax memorized.
contains(String text) - Returns true if the String contains this text, false otherwise..
if(word.contains("ice"))
indexOf(String text) - Returns the index position of the first location a String has text. If not found, returns -1.
int index = word.indexOf("sword");
replace(String a, String b) - Returns a copy of the String with
int index = word.indexOf("sword");
These are just a few of Mr. M's favorites. Want more? Here is a longer list of handy string methods.
Concatenation
Sometimes you will want to put two Strings together to make a bigger String. To do this, we'll use the + (plus) operator. This process is called concatenation.
String a = "kit" + "ten" // a is kitten
String b = a + "s love Strings!" // b is kittens love Strings!
RESOURCES