bhylu.com

Advertisements

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.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

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
8

The 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 termWhat 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, compareToIgnoreCaseLexicographic comparisons; these are not locale-sensitive human collation.
concat, joinJoin strings; join is static and inserts a supplied delimiter between elements.
contains, startsWith, endsWithLiteral presence, prefix and suffix checks without regex interpretation.
contentEqualsCompare against a CharSequence or supported buffer overload rather than only another String.
copyValueOf, valueOfStatic conversions or copies into a String; choose the overload matching the source type.
equals, equalsIgnoreCaseContent equality checks; a null argument is not equal to a non-null receiver.
formatStatic formatted text construction; use an explicit Locale for stable locale-sensitive output.
getBytes, getCharsEncode text into bytes with a chosen charset, or copy a character range into a destination array.
hashCodeValue hash used with equality contracts; not a cryptographic digest or unique identifier.
indexOf, lastIndexOfReturn a matching position, or -1; range and starting-position overloads change the search.
isEmpty, isBlankDistinguish zero length from text consisting of whitespace under the documented rule.
lengthCounts UTF-16 code units; supplementary points can occupy two.
matchesTests the complete String against a regex, not merely the presence of a matching region.
regionMatchesCompare a selected region, with an optional case-insensitive form.
replaceLiteral character or CharSequence replacement; the source String remains unchanged.
replaceAll, replaceFirstRegex replacement; replacement backslashes and dollar signs have their own meaning.
splitRegex tokenisation; a negative limit can preserve trailing empty fields.
subSequence, substringHalf-open code-unit slicing; validate boundaries and know the return type.
toCharArrayReturns a new array of UTF-16 code units.
toLowerCase, toUpperCaseCase conversion can depend on locale and can change length; name a locale deliberately.
toStringA String’s string representation is itself text, not a copy/parse operation.
trim, striptrim 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.

Advertisements