Strings

Also known as text.

Strings are yet another kind of value

They are not a primitive type but they do get some special support in the language.

Literal strings.

"foo"

"a string with spaces"

Note that the quotation marks are not part of the string.

"foo" is three characters long, not five.

What can we do with strings?

  • Smoosh them together

  • Take them apart

  • Make related strings

The + operator works on strings

Also known as “concatenation”

» "foo" + "bar"
"foobar"

⚠️ Watch out though ⚠️

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.

» 1 + "0"
"10"

Indexes

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.

The length method

Strings have a length() method that returns the number of characters in the String.

» "foobar".length()
6

length() vs length

Note that length() is a method on String whereas arrays have a length property.

someString.length()

someArray.length

Valid indexes for a string s

If 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 method

Sometimes we want to extract a chunk of a string.

Strings have a substring method for this.

Extracts a substring

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().

indexOf

This 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.

» foobar".indexOf("a")
4

» "foobar".indexOf("z")
-1

s = "foobar"

» s.substring(0)
"foobar"

» s.substring(1)
"oobar"

» s.substring(s.length() - 1)
"r"

» s.substring(s.length())
""

» s.substring(0, 3)
"foo"

» s.substring(3)
"bar"

» s.substring(3, s.length())
"bar"

» s.substring(s.indexOf("b"))
"bar"

String equality

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.

String comparisons

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.

Just compare to zero

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

Splitting strings

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" }

🚨 Warning 🚨

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

Some other useful String methods

» "foo".toUpperCase()
"FOO"

» "FOO".toLowerCase()
"foo"

Chaining methods

If s is a string

s.substring(1) is also a string

s.substring(1).toUpperCase()

Can use any string expression

"foo" + "bar".toUpperCase() // "fooBAR"

("foo" + "bar").toUpperCase() // "FOOBAR"

f = "foo"

f.substring(0, 1).toUpperCase() + f.substring(1) // "Foo"

Cheat sheet

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"