String Methods
We originally explained that Strings are primitive types, and that is true. However, they are functionally Objects. We can treat Strings like objects, but this also means that we can use additional methods to manipulate them.
substring()
Substring allows us to break apart any string and pull sub-strings from it (get it?). But before we go into that, we need to talk about ordering in Java.
Ordering
When you count, you normally count like this: 1,2,3,4,5.... It makes sense, right?
But in Java (and many other programming languages), we count like this: 0,1,2,3,4....
0 is the first item. 1 is the second item. 2 is the third item. And so on and so on.
This is called indexing. In the word "Hello", H has an index of 0. e has an index of 1. l has an index of 2.
Keep this in mind as we go into substring()
Using Substring
Now, let's use substring. With this string, "Hello World", let's get the word "Hello".
We take the index of H, 0, and the index of the character after where we want the word to end, so the space, which has an index of 5.
String word = "Hello World";
System.out.println(word.substring(0,5));
This prints: Hello. Just like we wanted.
Lucky for us, substring is overloaded; If we put one parameter, we get all the characters after that index, including the character at the index:
String word = "Hello World";
System.out.println(word.substring(6));
This prints World. Remember, we cannot use negative indices (the plural of an index). So if we want to get the characters at the end, we need to do something else.
length()
If you want to know how many characters are in a String, we can use length(). It returns the int of characters in the String.
System.out.println("word".length());
We can use this in two ways:
substring()
With length() and substring(), we can now get the last few characters of a string.
For example, if we wanted the last two characters of a string:
String word = "abcxyzhi";
System.out.println(word.substring(word.length()-2));
Loops
Let's try this in a for loop (you can do this with a while loop, but we know the number of iterations/loops):
String word = "Hi there!";
for(int i = 0; i < word.length(); i++) {
System.out.println(word.substring(i,i+1));
}
This loop prints each character in word on its own line.
equals()
We've already talked about .equals(), but did you know it can be used on any object? It's not just exclusive to Strings.
We'll use it much more in upcoming chapters and lessons, but here's a quick refresher:
String word1 = "Apple";
String word2 = "Apple";
String word3 = "Orange";
System.out.println(word1.equals("Apple"));
System.out.println("Apple".equals(word1));
System.out.println(word1.equals(word2));
System.out.println(word3.equals(word1));
You should get:
true
true
true
false
compareTo()
Whereas we use equals() to check if Strings/Objects are equal to each other, we can use compareTo() to make a deeper comparison.
If the string is equal to the other string, 0 will be returned.
If the string is lexicographically less (fewer characters) than the other string, a value less than0 will be returned.
If the string is lexicographically greater (more characters) than the other string, a value greater than0 will be returned.
You don't need to know anything about lexicography.
String word1 = "Apple";
String word2 = "Apple";
String word3 = "Orange";
String word4 = "Pear";
System.out.println(word1.compareTo("Apple"));
System.out.println(word1.compareTo(word2));
System.out.println(word3.compareTo(word1));
System.out.println(word3.compareTo(word4));
You should get:
0
0
14
-1
toLowerCase() and toUpperCase()
And finally, toLowerCase() and toUpperCase(). These do exactly what you think they do.
toLowerCase() makes the entire string lowercase.
toUpperCase() makes the entire string uppercase.
String phrase = "ThEsE aRe WoRdS";
System.out.println(phrase.toLowerCase());
System.out.println(phrase.toUpperCase());
And the result:
these are words
THESE ARE WORDS