Java String Methods

About This Index
This page is the Java String Methods index: every method of java.lang.String covered in this tutorial track, each with a full guide covering syntax, all overloads, parameters, return value, exceptions, three runnable examples, and FAQs. Every example is a complete Java class with fixed input, so the output shown is the output you get.
Learn what a String is on the Java Strings page. Use this page when you need a specific method such as substring(), indexOf(), split(), or replace(). Examples compile on Java 8 and later, except for methods that are themselves newer; their pages say which version they need.
A String is immutable. Methods that seem to edit text, such as replace(), trim(), or toUpperCase(), return a new String and leave the original as it was. Compare text with equals(), not ==.
How to Call a String Method
Instance methods run on a String value; static helpers use the class name:
String text = " Hello, Java! ";
// Instance methods (called on a value)
String clean = text.trim(); // "Hello, Java!"
int size = clean.length(); // 12
int comma = clean.indexOf(','); // 5
String word = clean.substring(7, 11); // "Java"
String[] bits = "a,b,c".split(","); // [a, b, c]
// Static methods (called on the class)
String joined = String.join("-", "2026", "11", "19"); // "2026-11-19"
String number = String.valueOf(42); // "42" Java String Methods Table
All method guides in this Java tutorial series (55 tutorials), grouped by purpose.
Characters & Unicode
9 methodsRead single characters by index, walk them as streams, copy them into arrays, and handle emoji and other code points.
| Method | Description | Tutorial |
|---|---|---|
charAt() | Read the char at a zero-based index. | Try it |
chars() | Stream the characters of a string as an IntStream of UTF-16 values. | Try it |
codePointAt() | Read the full Unicode code point that starts at an index. | Try it |
codePointBefore() | Read the Unicode code point just before an index. | Try it |
codePointCount() | Count real Unicode characters (code points) in a range. | Try it |
codePoints() | Stream the Unicode code points of a string, emoji included. | Try it |
offsetByCodePoints() | Move an index forward or back by whole Unicode characters. | Try it |
toCharArray() | Copy the characters into a brand-new char array. | Try it |
getChars() | Copy a range of characters into an existing char array. | Try it |
Comparison & ordering
7 methodsEquality, case rules, dictionary order, partial region comparison, and hash codes.
| Method | Description | Tutorial |
|---|---|---|
compareTo() | Compare two strings in dictionary order (case-sensitive). | Try it |
compareToIgnoreCase() | Compare two strings in dictionary order, ignoring case. | Try it |
equals() | Compare the text of two strings (case-sensitive). | Try it |
equalsIgnoreCase() | Compare the text of two strings, ignoring case. | Try it |
contentEquals() | Compare a String with any CharSequence, such as a StringBuilder. | Try it |
regionMatches() | Compare a part of one string with a part of another. | Try it |
hashCode() | Get the int hash code computed from the characters. | Try it |
Search & match
6 methodsCheck for a prefix, suffix, or substring, find its position, or test the whole string against a regex.
| Method | Description | Tutorial |
|---|---|---|
contains() | Check whether a piece of text appears anywhere in a string. | Try it |
startsWith() | Check whether a string starts with a prefix, optionally at an offset. | Try it |
endsWith() | Check whether a string ends with a suffix. | Try it |
indexOf() | Find the first position of a char or substring (-1 if absent). | Try it |
lastIndexOf() | Find the last position of a char or substring (-1 if absent). | Try it |
matches() | Check whether the WHOLE string matches a regular expression. | Try it |
Length, emptiness & slices
5 methodsMeasure a string, test for empty or blank text, and cut out a part of it.
| Method | Description | Tutorial |
|---|---|---|
length() | Get the number of UTF-16 chars in a string. | Try it |
isEmpty() | Check whether a string has length 0. | Try it |
isBlank() | Check whether a string is empty or only whitespace (Java 11+). | Try it |
substring() | Get a slice of a string by start and end index. | Try it |
subSequence() | Get a slice of the string typed as a CharSequence. | Try it |
Replace & change case
5 methodsSwap characters or text, rewrite regex matches, and convert letter case. Each call returns a new String.
| Method | Description | Tutorial |
|---|---|---|
replace() | Replace every occurrence of a char or literal text. | Try it |
replaceAll() | Replace every regex match, with $1-style group references. | Try it |
replaceFirst() | Replace only the first regex match. | Try it |
toLowerCase() | Convert every letter to lowercase (pass a Locale to be safe). | Try it |
toUpperCase() | Convert every letter to uppercase (pass a Locale to be safe). | Try it |
Trim, strip & indentation
6 methodsRemove whitespace from the ends, or add and remove indentation line by line.
| Method | Description | Tutorial |
|---|---|---|
trim() | Remove spaces and control characters (up to U+0020) from both ends. | Try it |
strip() | Remove Unicode whitespace from both ends (Java 11+). | Try it |
stripLeading() | Remove Unicode whitespace from the start only (Java 11+). | Try it |
stripTrailing() | Remove Unicode whitespace from the end only (Java 11+). | Try it |
stripIndent() | Remove the common leading indentation from every line (Java 15+). | Try it |
indent() | Add or remove leading spaces on every line (Java 12+). | Try it |
Split, join & lines
4 methodsBreak text into pieces around a delimiter or line breaks, and glue pieces back together.
Build, convert & format
9 methodsAppend and repeat text, fill templates, turn values or char arrays into strings, encode bytes, and process escapes.
| Method | Description | Tutorial |
|---|---|---|
concat() | Append one string to the end of another. | Try it |
repeat() | Repeat a string n times (Java 11+). | Try it |
format() | Build a string from a template with %s, %d, and %.2f (static). | Try it |
formatted() | Fill this string as a format template, like String.format (Java 15+). | Try it |
valueOf() | Convert numbers, booleans, chars, objects, or char arrays to a String (static). | Try it |
copyValueOf() | Build a String from a char array (static). | Try it |
getBytes() | Encode a string into a byte array with a charset. | Try it |
translateEscapes() | Turn escape sequences such as \n and \t into real characters (Java 15+). | Try it |
transform() | Pass the string to a function and return its result (Java 12+). | Try it |
Identity, string pool & constants
4 methodsThe same object, the shared pool copy, and the advanced constant API used by compilers and frameworks.
| Method | Description | Tutorial |
|---|---|---|
toString() | Return the string itself (the same object). | Try it |
intern() | Get the shared copy of a string from the string pool. | Try it |
describeConstable() | Get an Optional holding the string as its own constant descriptor (Java 12+). | Try it |
resolveConstantDesc() | Resolve the string as a constant descriptor: returns itself (Java 12+). | Try it |
Frequently Asked Questions
Start with charAt()
Read a single character by its index, the first step in working with text.
15 people found this page helpful
