Strings
Strings in Java are not character arrays, unlike in C and C++.
There are two string classes:
- String defines read-only strings and operations on them.
- StringBuffer defines mutable strings.
Since strings are no arrays, one cannot access characters in a string
by means of the array syntax, like str[i].
- The length() method returns the string length.
- The charAt(int i) method returns the character at
position i.
- Operations similar to the C functions for characters and strings
also exist:
- indexOf(char ch) returns the first position of ch.
- indexOf(char ch, int start) returns the first position of ch after start.
- indexOf(String s) returns the first position of s.
- indexOf(String s, int start returns the first position of s after start.
- lastIndexOf(char ch) returns the last position of ch.
- etc.
- equals(String s) tests string equality.
- equalsIgnoreCase(String s) tests string equality, up to
the difference between upper- and lowercase.
- regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) tests whether two substrings are equal.
- substring(int beginIndex, int endIndex) generates a substring.
- etc.
Note that characters are 16 bit Unicode. Matching upper- and lowercase is
complicated.
- append(...) appends a string representation of a
boolean, character, integer, float, etc. to a stringbuffer.
- insert(int pos, ...) inserts a string representation of a
boolean, character, integer, float, etc. into a stringbuffer at position
pos.
- toString() converts a stringbuffer to a string.