Incremental Java
Math Strings

Math Strings

Before we explain how Java strings work, we learn about mathematical strings.

First, we define an alphabet. An alphabet is a set of characters. An alphabet doesn't have to resemble the English alphabet. For example, { '0', '1' } could be an alphabet. It just has two characters.

A string is zero or more characters from the alphabet.

We write strings starting with a double quote (called the open quote) and ending with a double quote. The quotes are not part of the string. They only tell you where the string begins and ends.

Examples

Assume the alphabet consists of uppercase letters, lowercase letters, digits and a blank space.

Here are examples of valid strings: "", "ab", "The quick brown fox jumped over the lazy dog".

"" is called the empty string.

Length of a String

The length of a string is the number of characters in the string. Thus, "cat" has length 3, "" has length 0, and "cat " has length 4. Notice that spaces count in the length, but the double quotes do not.

If we have escape sequences in the alphabet, then they count as one character. Thus, "\n\n\n" has length 3. Yes, you typed 6 characters, but when Java runs the program, it treats '\n' as one character, not two. "\\\\\\" also has length 3 (there are three escaped backslashes in this string).

Indexing a String

We may want to refer to a specific character in the string. To do this, we number the characters left to right (including spaces) starting with 0.

Thus, in "ca\nt", we have 'c' at index 0, 'a' at index 1, '\n' at index 2, and 't' at index 3. Again, notice how escape sequences only count as one character.

Notice that we don't put the single quotes in the string.

Substring

A substring of a string consists of a start index and an end index. The start index is the index of the first character. The end index is the index of the last character + 1.

It may seem strange that the end index is not the index of the last character, but this allows us to specify an empty string.

Consider the string "tomcatfoolery". "cat" is a substring of this string. It has start index 3 and end index 6 (which is the index of 'f').

On the other hand "tfool" is not substring of "tomfoolery", since there is no start index and end index that exactly picks these letters out (and no others).

The entire string is usually considered a substring of itself.

The empty string is always a substring of any string, even the empty string.

There are two useful operations with substrings. You can ask if some string is a substring of another. You can also give a start and end index to extract a substring from a string.

In Java, extracting a substring creates a new string. The original string is unchanged.

Concatenation

To concatenate means to put together. In this case, we wish to put together two strings to make a third.

We use the + operator (just as Java does) to concatenate strings. Thus, "slime" + "dog" creates a new string "slimedog". We can concatenate several strings. For example, "slime" + "dog" + "news" evaluates to "slimedognews".

Concatenation creates a new string. Neither of the strings that are operands in concatenation are changed.