Strings

A string literal is represented by sequence of characters in double quotes.

The String class implements immutable string. Most JVM are cahing string values, but not always.

The StringBuilder class implements mutable string. It is used hiddenly in expressions with string concatenation by the + operator. So do not use + with strings in a long loop. Instead, create a StringBuilder before the loop, and then use it inside.

The StringBuffer class is a thread-safe version of StringBuilder.

All mentioned classes implements the CharSequence interface.

The StringJoiner class is used to construct a sequence of characters separated by a delimiter. Optionally, you can specify a prefix and suffix.

String str = "my string";
String str1= "a" + "b"; // result "ab", StringBuilder is used

StringBuilder sb = new StringBuilder()
    .append("sb ");
// ...     
sb.append("blablabla");
String str3 = sb.toString();

StringJoiner sj = new StringJoiner(",","(",")");
for(String item: mystrings){
    sj.add(item)
}
// result is string such "(item1,item2,...,itemn)"
String str4 = sj.toString();

if("m".equals(mystr)){...} // don't compare strings with == operator

escape sequences

esc. seq. description
\" Inserts a double quote.
\' Inserts a single quote.
\\ Inserts a backslash.
\n Inserts a new line.
\r Inserts a carriage return.
\v Inserts a vertical tab.
\t Inserts a tab.
\f Form feed.
\uXXXX Inserts a unicode character with code XXXX (4 hex digits).

CharSequence interface

method description
charAt(ind) Returns the char value at the specified index.
chars() Returns a stream of int zero-extending the char values from this sequence.
codePoints() Returns a stream of code point values from this sequence.
length() The number of characters in the sequence.
subSequence(start, end) Returns a subsequence from range [start, end).
toString() Returns string with this sequence of characters.

String

method description
instance methods
charAt(ind) Returns the char value at the specified index.
codePointAt(ind) Returns the character (Unicode code point) at the specified index.
codePointBefore(ind) Returns the character (Unicode code point) before the specified index.
codePointCount(start, end) Returns the number of Unicode code points in the specified text range.
compareTo(str) Compares two strings lexicographically.
compareToIgnoreCase(str) Compares two strings lexicographically, ignoring case differences.
concat(str) Concatenates the specified string to the end of this string.
contains(chSeq) Returns true if and only if this string contains the specified sequence of char values.
contentEquals(chSeq) Compares this string to the specified CharSequence.
contentEquals(sb) Compares this string to the specified StringBuffer.
endsWith(suffix) Tests if string ends with the specified suffix.
equals(obj) Compares this string to the specified object.
equalsIgnoreCase(str) Compares this string to another string, ignoring case considerations.
getBytes() Encodes into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
getBytes(charset) Encodes into a sequence of bytes using the given charset, storing the result into a new byte array.
getBytes(charsetName) Encodes into a sequence of bytes using the named charset, storing the result into a new byte array.
getChars(begin, end, dst, dstBegin) Copies characters from range [begin, end) to the given character array.
hashCode() Returns a hash code for this string.
indexOf(ch) Returns the index of the first occurrence of the specified character, or -1 if the character does not occur.
indexOf(ch, pos) Returns the index of the first occurrence of the specified character from the given position, or -1 if the character does not occur.
indexOf(str) Returns the index of the first occurrence of the specified substring, or -1 if the substring does not occur.
indexOf(str, pos) Returns the index of the first occurrence of the specified substring from the given position, or -1 if the substring does not occur.
intern() Returns a canonical representation for the string object.
isEmpty() Returns true if length() is 0.
lastIndexOf(ch) Returns the index of the last occurrence of the specified character.
lastIndexOf(ch, pos) Returns the index of the last occurrence of the specified character, or -1 if the character does not occur. The searching is backward from the given position to the beginning of string.
lastIndexOf(str) Returns the index of the last occurrence of the specified substring, or -1 if the substring does not occur.
lastIndexOf(str, pos) Returns the index of the last occurrence of the specified substring, or -1 if the substring does not occur. The searching is backward from the given position to the beginning of string.
length() Returns the length of this string.
matches(regex) Returns true if string matches the given regular expression.
regionMatches(ignoreCase, offset, str, strOffset, len) Tests if two string regions are equal.
regionMatches(offset, str, strOffset, len) Tests if two string regions are equal.
replace(ch, newCh) Replaces all occurrences of ch with the character newCh.
replace(target, replacement) Replaces all occurrences of the target substring with the substring replacement.
replaceAll(regex, replacement) Replaces each substring that matches the given regular expression with the given replacement.
replaceFirst(regex, replacement) Replaces the first substring that matches the given regular expression with the given replacement.
split(regex) Splits this string around matches of the given regular expression.
split(regex, limit) Splits this string around matches of the given regular expression.
startsWith(prefix) Returns true if string starts with the specified prefix.
startsWith(prefix, pos) Returns true if string starts with the specified prefix at the specified position.
subSequence(begin, end) Returns a character sequence from the specified range.
substring(begin) Returns substring from the specified position.
substring(begin, end) Returns a substring from specified range [begin, end).
toCharArray() Converts string to a new character array.
toLowerCase() Converts all of the characters in this string to lower case using the rules of the default locale.
toLowerCase(locale) Converts all of the characters in this string to lower case using the rules of the given Locale.
toString() Returns itself.
toUpperCase() Converts all of the characters in this string to upper case using the rules of the default locale.
toUpperCase(locale) Converts all of the characters in this String to upper case using the rules of the given Locale.
trim() Returns a string without any leading and trailing whitespace.
static methods
join(delimiter, ... elements) Returns a new string composed of the elements joined together with the specified delimiter.
join(delimiter, itElements) Returns a new string composed of elements joined together with the specified delimiter.
format(locale, format, ... args) Returns a formatted string using the specified locale, format string, and arguments.
format(format, ... args) Returns a formatted string using the specified format string and arguments.
copyValueOf(char[] data) Equivalent to valueOf(char[]).
copyValueOf(char[] data, offset, count) Equivalent to valueOf(char[], int, int).
valueOf(boolean b) Returns the string representation of the boolean argument.
valueOf(char c) Returns the string representation of the char argument.
valueOf(char[] data) Returns the string representation of the char array argument.
valueOf(char[] data, offset, count) Returns the string representation of a specific subarray of the char array argument.
valueOf(double d) Returns the string representation of the double argument.
valueOf(float f) Returns the string representation of the float argument.
valueOf(int i) Returns the string representation of the int argument.
valueOf(long l) Returns the string representation of the long argument.
valueOf(Object obj) Returns the string representation of the Object argument.