Java: Summary - String

String Concatenation

The + operator joins two strings together. If either operand is String, the other is converted to String and concatenated with it. This is a common way to convert numbers to Strings.

If a non-String object is concatenated with a String, its toString() method is called. When you write a class that might have a sensible representation as a string, it's useful for debugging purposes to override Object's useless toString() method.

"abc" + "def""abcdef"
"abc" + 4 "abc4"
"1" + 2 "12"
"xyz" + (2+2 == 4)"xyztrue"
1 + "2.5" "12.5"

String methods

These are common String methods. In all of these prototypes, i and j are int indexes into a string, s and t are Strings, and c is a char. cs is a CharacterSequence (eg, either String or StringBuilder).
Length
i = s.length() length of the string s.
Comparison (note: use these instead of == and !=)
i = s.compareTo(t) compares to s. returns <0 if s<t, 0 if ==, >0 if s>t
i = s.compareToIgnoreCase(t) same as above, but upper and lower case are same
b = s.equals(t) true if the two strings have equal values
b = s.equalsIgnoreCase(t) same as above ignoring case
b = s.startsWith(t) true if s starts with t
b = s.startsWith(t, i) true if t occurs starting at index i
b = s.endsWith(t) true if s ends with t
Searching -- Note: All "indexOf" methods return -1 if the string/char is not found
i = s.contains(cs) True if cs can be found in s.
i = s.indexOf(t) index of the first occurrence of String t in s.
i = s.indexOf(t, i) index of String t at or after position i in s.
i = s.indexOf(c) index of the first occurrence of char c in s.
i = s.indexOf(c, i) index of char c at or after position i in s.
i = s.lastIndexOf(c) index of last occurrence of c in s.
i = s.lastIndexOf(c, i) index of last occurrence of c on or before i in s.
i = s.lastIndexOf(t) index of last occurrence of t in s.
i = s.lastIndexOf(t, i) index of last occurrence of t on or before i in s.
Getting parts
c = s.charAt(i) char at position i in s.
s1 = s.substring(i) substring from index i to the end of s.
s1 = s.substring(i, j) substring from index i to BEFORE index j of s.
Creating a new string from the original
s1 = s.toLowerCase() new String with all chars lowercase
s1 = s.toUpperCase() new String with all chars uppercase
s1 = s.trim() new String with whitespace deleted from front and back
s1 = s.replace(c1, c2) new String with all c1 characters replaced by character c2.
s1 = s.replace(cs2, cs3) new String with all cs2 substrings replaced by cs3.
Regular Expressions (as of Java 1.4) regexStr parameters are Strings, but see also java.util.regex.Pattern, ...
b = s.matches(regexStr) true if regexStr matches the entire string in s. Same as Pattern.matches(regexStr, s)
s1 = s.replaceAll(regexStr, t) replaces each substring that matches regexStr with String t
s1 = s.replaceFirst(regexStr, t) replaces first substring that matches regexStr with String t
sa = s.split(regexStr) array of all substrings terminated by regexStr or end
sa = s.split(regexStr, count) limited to applying regexStr only count times.
Static Methods for Converting to String
s = String.valueOf(x) Converts x to String, where x is any type value (primitive or object).
s = String.format(f, x...) [Java 5] Use format f to convert variable number of parameters, x to a string.
Comparator for sorting arrays and collections.
comp = String.CASE_INSENSITIVE_ORDER A static Comparator object that does case-insensitive comparisons for sorts and ordered Collections (eg, TreeMap, TreeSet, SortedMap, and SortedSet).

Related classes and interfaces

StringBuilder and StringBuffer are much more efficient when building up a string from many strings.
Character has many useful static methods for working with single characters.
Pattern and Matcher provide full regular expression processing.
CharSequence interface is implemented by String, StringBuilder and others. Defines length(), charAt(pos), subSequence(beg, upto), and toString().
StringTokenizer divides strings into tokens, but String.split(regex) and other regular expression methods are more useful.
StringCharacterIterator and CharacterIterator don't seem to be especially useful.


Copyleft 2006 Fred Swartz