5.10. Strings


Type String

A value of type String is a sequence of zero or more characters. Write a literal string in double-quotes, such as "this is a string".


Operations on strings

s.length()

This yields the number of characters in string s. For example, expression "cat".length() yields 3.

s.charAt(n)

This yields the n-th character of string s, numbering from 0. For example, "cat".charAt(0) yields 'c' and "cat".charAt(1) yields 'a'.

s + t

This yields the concatenation of strings s and t. For example, expression "abc" + "def" yields "abcdef".

You can concatenate a string with any type of value. The non-string value is automaticallyconverted to a string. For example,

 int    n   = 50;
 String str = "n = " + n;
makes str = "n = 50".

A simple way to convert a value to a string without adding any additional characters is to concatenate it with an empty string. Expression "" + 250 yields "250".


s.equals(t)

This yields true if strings s and t are the same (have the same length and exactly the same characters, in the same order.) For example, "cat".equals("cat") yields true, but "cat".equals("cave") yields false.

Be careful when comparing strings. Expressions s == t does not correctly compare strings s and t.


s.compareTo(t)

This yields 0 if s and s are equal, a negative number if s comes lexically before t and a positive number if s comes lexically after t. For example, "cat".compareTo("cute") yields a negative number because "cat" is alphabetically before "cute".

Note. Lexicographic order is defined with respect to the order of the Unicode alphabet. Since 'C' comes before 'a', you find that "Cat".compareTo("ant") yields a nagative number.

If one of the strings is a prefix of the other, then the shorter one is considered to come first in lexicographic order.


s.compareToIgnoreCase(t)

Like compareTo, but ignore the case of letters. For example, treat 'a' as the same as 'A'.

s.indexOf(ch)

This yields the index of the first occurrence of character ch in string s, or −1 if ch does not occur in s. Indexing is from 0. For example, "abcd".indexOf('c') yields 2.

s.indexOf(t)

This yields the start index of the first occurrence of string t within s, or −1 if t does not occur in s. Indexing is from 0. For example, "forward".indexOf("war") yields 3.

s.substring(i, j)

This yields the substring of string s starting at index i and ending at index j−1. Indexing is from 0. For example, "forward".substring(2,5) yields "rwa".

s.substring(i)

This yields the substring of s that is obtained by removing the first i characters of s.

s.startsWith(t)

This is true if t is a prefix of s. For example, "forward".startsWith("for") yields true. Every string is a prefix of itself, so "forward".startsWith("forward") also yields true.

s.endsWith(t)

This is true if t is a suffix of s. For example, "forward".endsWith("ward") yields true. Every string is a suffix of itself.

s.toCharArray()

Return an array of characters holding exactly the characters in string s.


Constructors for strings

new String(a)

This yields a string containing the characters in array-of-characters a.


Watch out for extra quotes

Double-quote marks indicate a literal string. Do not add quote-marks as decoration to say that something is a string. For example, statements

  String a = "camel";
  String b = a;
makes b = "camel", but
  String a = "camel";
  String b = "a";
makes b = "a".