Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, November 19, 2007

java 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 ="=",">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).

No comments:

Post a Comment

Archives