Strings Flashcards
Constructor
- String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
- String(char[] value, int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument. The offset argument is the index of the first character of the subarray and the count argument specifies the length of the subarray.
- String(byte[] bytes)
Constructs a new String by decoding the specified array of bytes using the platform’s default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
- String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument. This constructor is provided to ease migration to StringBuilder. Obtaining a string from a string builder via the toString method is likely to run faster and is generally preferred.
compareTo/compareToIgnoreCase
- compareTo(String anotherString)
Compares two strings lexicographically.
valueOf
- valueOf(boolean b)
Returns string representation of boolean/int/long/float/double/char/char[] argument.
charAt
- charAt(int index)
Returns the char value at the specified index.
indexOf
- indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.
- indexOf(int ch, int fromIndex)
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
- indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
- indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
lastIndexOf
- lastIndexOf(int ch)
Returns the index within this string of the last occurrence of the specified character.
- lastIndexOf(int ch, int fromIndex)
Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.
- lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring.
- lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
Length of String
length()
copyValueOf
- copyValueOf(char[] data)
Returns a String that represents the character sequence in the array specified.
- copyValueOf(char[] data, int offset, int count)
Returns a String that represents the character sequence in the array specified.
- It is equivalent to valueOf(), so use that.
toCharArray() and trim()
- toCharArray()
Converts this string to a new character array.
- trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
replace/replaceAll/replaceFirst
- replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
- replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
- replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
- replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
split
- returns a String array (String[])
- split(String regex)
Splits this string around matches of the given regular expression.
split(String regex, int limit)
Splits this string around matches of the given regular expression.
- The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
- String[] result = “this is a test”.split(“\s”);
- for (int x=0; xSystem.out.println(result[x]);
prints the following output:
- this
- is
- a
- test
StringJoiner
- StringJoiner(CharSequence delimiter)
Constructs a StringJoiner with no characters in it, with no prefix or suffix, and a copy of the supplied delimiter.
- StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Constructs a StringJoiner with no characters in it using copies of the supplied prefix, delimiter and suffix.
- The String “[George:Sally:Fred]” may be constructed as follows:
StringJoiner sj = new StringJoiner(“:”, “[”, “]”); sj.add(“George”).add(“Sally”).add(“Fred”); String desiredString = sj.toString();
concat syntax?
- concat(String str)
Concatenates the specified string to the end of this string.
- If a is null, then a.concat(b) throws a NullPointerException but a += b will treat the original value of a as if it were null.
- Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.
contains syntax?
- contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
startsWith/endsWith
- startsWith(String prefix)
Tests if this string starts with the specified prefix.
startsWith(String prefix, int toffset)
Tests if the substring of this string beginning at the specified index starts with the specified prefix.
- endsWith(String suffix)
Tests if this string ends with the specified suffix.