Characters and Strings


Characters

Values of type char are characters. Write them the same way you write then in Cinnameg, in single quote marks. For example, 'x' is the lower case x character.

There is an important difference in alphabets between Cinnameg and Java. Cinnameg uses the Ascii alphabet, which has only 128 characters. But Java uses the Unicode alphabet, which has thousands of characters, including characters from all of the major languages of the world. The Ascii alphabet is a subset of the Unicode alphabet, and we will restrict ourselves to characters in that alphabet.


Strings and string constants

A string is an object of class String. You write an explicit string in double-quote marks, exactly as in Cinnameg. For example, "radius" is an object of class String.


String capabilities

The following table describes some of the operations that are available on strings.

Operation Description
s + t

Operator + concatenates strings, which means it glues them together into one string. (It is roughly equivalent to ++ in Cinnameg.) For example, "abc" + "def" = "abcdef".

Java allows you to concatenate a string with something that is not a string. The nonstring is automatically converted to a string. For example, expression

  int x = 20;
  String r = "I am " + x + " years old"
makes r = "I am 20 years old". Notice that it needed to covert the integer 20 into a string of two characters (a '2' and a '0').

s.length( )

Expression s.length( ) yields the length of string s. For example, "horse".length( ) = 5.

s.charAt(n)

In Java, the characters in a string are numbered starting at 0. s.charAt(n) is the (n+1)st character in string s. For example, "disk".charAt(0) = 'd' and "disk".charAt(1) = 'i'. It is important to get used to the idea that the first character in a string is character number 0, the second is character number 1, etc.

(If there is no character with index n in s, then s.charAt(n) causes an error. There is more on this error in a later chapter.)

s.substring(a, b) s.substring(a, b) is the substring of string s from index a to index b-1. But remember that the indices start at 0. For example, "rabbit".substring(1,5) = "abbi", the characters from index 1 to index 4, where the first character is considered character number 0. Don't forget that the substring includes the first index but does not include the second index.
s.indexOf(t) This finds the first occurrence of string t inside string s, and yields the index where the substring begins (where the first character has index 0). For example, "the bear at the berries".indexOf("the") = 0 and "the bear at the berries".indexOf("at") = 9.

If string t does not occur in string s, then s.indexOf(t) yields -1.

s.equals(t) You are allowed to ask whether two strings are equal using operator ==. But expression s == t is true if s and t are the same object. It is possible to create two different string objects that are remembering exactly the same sequence of characters, and operator == will say that those objects are not equal.



Expression s.equals(t) is true if strings s and t contain exactly the same characters, in the same order. You should almost always use that to test whether two strings are equal.
s.compareTo(t) If s and t are two strings, then s.compareTo(t) is
-1 if s is alphabetically before t,
0 if s and t have exactly the same characters,
1 if s is alphabetically after t.
For example, expression "rabbit".compareTo("camel") has value 1. Expression s.equals(t) has the same boolean value as expression s.compareTo(t) == 0.
x.toString( ) You can convert most values to a string using the toString method. For example, 51.toString() = "51".

Strings never change

Some types of object have methods that tell the object to alter the information that it is remembering. Strings are not that kind of object.

There are no methods that tell a string object to remember something different. You can build new string objects, but never modify existing ones. So if you get the idea that a particular method modifies a string object, think again. (Some of the Java documentation is unclear, and can lead you to believe that a particular method modifies a string. For example, the Java documentation for string method replace says that s.replace(target, replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
By this string, they mean the string object s that is asked to do the replace operation. From the documentation, it appears that this method changes the information in a string. In fact, it builds a new string object. For example,
  String s = "I am happy";
  String t = s.replace("happy", "ecstatic");
makes t refer to string "I am ecstatic". Variable s still refers to string "I am happy". The documentation has been written rather tersely for people who already understand that strings never change.


More information

Strings are discussed in Chapter 2.2 of Savitch and Carrano.


Problems

  1. [solve] Write a Java definition of function isPrefix(s, t), which produces true if string s is a prefix of string t. For example, isPrefix("rab", "rabbit") should be true. According to our definition, a string is considered to be a prefix of itself. So isPrefix("rabbit", "rabbit") should also be true.

    (Hint: Just get a substring from the beginning of t that has the same length as s, and see if it is equal to s. Be careful, since it is possible that s is longer than t. Do not ask for an index in t that is too large.)

  2. [solve] Define function numBs(s) that produces the number of b's in string s. For example, numBs("bob") = 2. Only count lower case b's.

    (Hint. Use a loop. Look at each character. Keep a count of the number of b's.


Summary

Strings are objects. Write a string constant in double quotes.

Operations on strings are listed in the table above. You ask a string object to do an operation for you, so you use the dot notation.

Once a string object has been created, it never changes the information that it is remembering. It can tell you about what it is remembering, and it can produce new strings.


Review

An object remembers some information and can process requests either to get or to modify the information.

Write a dot after an object, followed by a request, to ask the object to process the request. For requests that have answers, you get an expression. For requests without answers, you get a statement.

Objects are not copied unless you make an explicit request to copy (or clone them). Expression x == y is true if variables x and y refer to the same object.