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 the most common way to convert numbers to Strings.

If a non-String object is part of the contatenation, its string representation is produced by calling its toString() method. When you write a class that might have a sensible representation as a string, it's often useful for debugging purposes to define a toString() method. If no such method is defined, the default version from Object is used.

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

String methods

These are some of the most 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.
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.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 c2s replaced by c1s.
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] Uses 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

StringBuffer and StringBuilder, Character, StringTokenizer, java.util.regex.Pattern and Matcher, ....


Copyleft 2005 Fred Swartz Last update: 2005-01-20