Java String Methods

Beginner
7 min read
Updated: Nov 2026
55 method guides
Method index

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.

Strings never change in place.

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:

java
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 methods

Read single characters by index, walk them as streams, copy them into arrays, and handle emoji and other code points.

MethodDescriptionTutorial
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 methods

Equality, case rules, dictionary order, partial region comparison, and hash codes.

MethodDescriptionTutorial
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 methods

Check for a prefix, suffix, or substring, find its position, or test the whole string against a regex.

MethodDescriptionTutorial
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 methods

Measure a string, test for empty or blank text, and cut out a part of it.

MethodDescriptionTutorial
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 methods

Swap characters or text, rewrite regex matches, and convert letter case. Each call returns a new String.

MethodDescriptionTutorial
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 methods

Remove whitespace from the ends, or add and remove indentation line by line.

MethodDescriptionTutorial
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 methods

Break text into pieces around a delimiter or line breaks, and glue pieces back together.

MethodDescriptionTutorial
split()Split a string into an array around a regex.Try it
splitWithDelimiters()Split around a regex and keep the delimiters (Java 21+).Try it
join()Join several strings with a delimiter between them (static).Try it
lines()Split text into a Stream of lines (Java 11+).Try it

Build, convert & format

9 methods

Append and repeat text, fill templates, turn values or char arrays into strings, encode bytes, and process escapes.

MethodDescriptionTutorial
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 methods

The same object, the shared pool copy, and the advanced constant API used by compilers and frameworks.

MethodDescriptionTutorial
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

They are the built-in methods of java.lang.String for reading characters, measuring, comparing, searching, cutting, replacing, splitting, joining, trimming, and formatting text. Most are called on a string variable, for example text.length().
This index lists 55 guides, from charAt() and chars() to trim() and valueOf(), grouped by what they do.
No. Java strings are immutable. Methods such as replace(), toUpperCase(), trim(), and substring() return a new String, so assign the result: text = text.trim();
isBlank, lines, repeat, strip, stripLeading, and stripTrailing need Java 11; indent, transform, describeConstable, and resolveConstantDesc need Java 12; formatted, stripIndent, and translateEscapes need Java 15; splitWithDelimiters needs Java 21.
Read Java Strings first. Then open length(), charAt(), equals(), substring(), indexOf(), split(), replace(), and trim() from the tables below.

Start with charAt()

Read a single character by its index, the first step in working with text.

charAt() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful