Also known as text.
They are not a primitive type but they do get some special support in the language.
"foo""a string with spaces"Note that the quotation marks are not part of the string.
"foo" is three characters long, not five.
Smoosh them together
Take them apart
Make related strings
+ operator works on stringsAlso known as “concatenation”
When asked to add different types of values, and one of them is a string, Java will convert the other one to a string and then concatenate them.
Identify characters within the string by their position.
Just like array indexes identify the elements of arrays.
The index of the first character is 0.
This is called a zero-based index.
Think of it as how many characters are ahead of the given character.
length methodStrings have a length() method that returns the number of characters in the String.
length() vs lengthNote that length() is a method on String whereas arrays have a length property.
someString.length()
someArray.length
sIf the index of the first character is 0 what is the last valid index?
s.length() - 1
There is no character in the string that has length() characters ahead of it.
substring methodSometimes we want to extract a chunk of a string.
Strings have a substring method for this.
From a starting index (inclusive) to an end index (exclusive)
s.substring(start index, end index)
or
s.substring(start index)
Second argument is implicitly s.length().
indexOfThis method takes a String argument and returns the index (an int) where the argument appears in the String it was called on.
If the argument is not present, returns -1.
Can use this fact to test whether a character is in a string.
s = "foobar"Remember that == mostly doesn’t do what you want.
Use the equals method to check if two String values are “the same”.
See also string equality slides.
Strings can be compared alphabetically with the compareTo method.
s1.compareTo(s2)
Return value is a bit odd: it’s a negative number if s1 goes before s2 alphabetically, zero if they are equals, and positive if s1 goes after s2.
| If you mean | Write |
|---|---|
s1 < s2 |
s1.compareTo(s2) < 0 |
s1 > s2 |
s1.compareTo(s2) > 0 |
s1 <= s2 |
s1.compareTo(s2) <= 0 |
s1 >= s2 |
s1.compareTo(s2) >= 0 |
split() method takes a String argument and uses it as a delimiter to split apart the String the method was called on:
"a,b,c".split(",") ⟹ { "a", "b", "c" }
Can split on empty string to get individual characters:
"abc".split("") ⟹ { "a", "b", "c" }
Beware that the argument is actually not treated as just a simple string but a pattern (called a regular expression) in which some characters have special meanings.
"a.b.c".split(".") ==> { } // dot matches anything
And:
"a*b*c".split("*") // star has a special meaning
Exception java.util.regex.PatternSyntaxException:
Dangling meta character '*' near index 0
If s is a string
s.substring(1) is also a string
s.substring(1).toUpperCase()
"foo" + "bar".toUpperCase() // "fooBAR"
("foo" + "bar").toUpperCase() // "FOOBAR"
f = "foo"
f.substring(0, 1).toUpperCase() + f.substring(1) // "Foo"
String s = "Foo";
s + s ⟹ "FooFoo"
s.substring(0, 1) ⟹ "F"
s.length() ⟹ 3
s.indexOf("o") ⟹ 1
s.equals("bar") ⟹ false
s.compareTo("bar") ⟹ 4 (positive)
s.compareTo("quux") ⟹ -11 (negative)
s.toUpperCase() ⟹ "FOO"
s.toLowerCase() ⟹ "foo"