Java String API Reference
Use this reference to choose between text inspection, literal replacement, regex processing and Unicode-aware boundaries. Most String operations return a value rather than changing the original text.
Understand the idea
Read a method’s index unit, end-boundary convention and no-match behaviour before using its result. String indexes normally count UTF-16 code units, not visible graphemes. Several methods have overloads with additional locale, offset or range controls. The table groups the names learners commonly encounter and highlights the contract that tends to matter at a call site; consult the linked Java 21 API for overload-specific details.
Run it locally
Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.
javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out MainA worked example
public class Main {
public static void main(String[] args) {
String title = " Clay lab ";
String clean = title.strip();
System.out.println(clean.substring(0, clean.indexOf(' ')));
System.out.println(clean.replace("lab", "studio"));
System.out.println(clean.codePointCount(0, clean.length()));
}
}
Expected output
Clay
Clay studio
8The stripped value is stored separately, the first slice excludes the space position, and literal replace returns a changed value without altering clean. The final count uses the complete code-unit range for a code-point count. This is a small selection of the reference contracts, not a test of every overload.
Contracts to look up
| API or language term | What to check |
|---|---|
| charAt(index) | Returns one UTF-16 code unit; index must be below length. |
| codePointAt(index), codePointBefore(index) | Read a Unicode code point at a code-unit boundary; before uses the position after the point. |
| codePointCount(begin, end), offsetByCodePoints(index, count) | Count code points in a half-open range or find a boundary by a code-point offset. |
| compareTo, compareToIgnoreCase | Lexicographic comparisons; these are not locale-sensitive human collation. |
| concat, join | Join strings; join is static and inserts a supplied delimiter between elements. |
| contains, startsWith, endsWith | Literal presence, prefix and suffix checks without regex interpretation. |
| contentEquals | Compare against a CharSequence or supported buffer overload rather than only another String. |
| copyValueOf, valueOf | Static conversions or copies into a String; choose the overload matching the source type. |
| equals, equalsIgnoreCase | Content equality checks; a null argument is not equal to a non-null receiver. |
| format | Static formatted text construction; use an explicit Locale for stable locale-sensitive output. |
| getBytes, getChars | Encode text into bytes with a chosen charset, or copy a character range into a destination array. |
| hashCode | Value hash used with equality contracts; not a cryptographic digest or unique identifier. |
| indexOf, lastIndexOf | Return a matching position, or -1; range and starting-position overloads change the search. |
| isEmpty, isBlank | Distinguish zero length from text consisting of whitespace under the documented rule. |
| length | Counts UTF-16 code units; supplementary points can occupy two. |
| matches | Tests the complete String against a regex, not merely the presence of a matching region. |
| regionMatches | Compare a selected region, with an optional case-insensitive form. |
| replace | Literal character or CharSequence replacement; the source String remains unchanged. |
| replaceAll, replaceFirst | Regex replacement; replacement backslashes and dollar signs have their own meaning. |
| split | Regex tokenisation; a negative limit can preserve trailing empty fields. |
| subSequence, substring | Half-open code-unit slicing; validate boundaries and know the return type. |
| toCharArray | Returns a new array of UTF-16 code units. |
| toLowerCase, toUpperCase | Case conversion can depend on locale and can change length; name a locale deliberately. |
| toString | A String’s string representation is itself text, not a copy/parse operation. |
| trim, strip | trim uses the historical at-or-below-U+0020 rule; strip follows its Unicode whitespace contract. |
Check the official Java documentation for detailed contracts and supported options.