All Java String Functions: The Complete Method List

Formulas & functions · Updated November 22, 2026

There is no single exhaustive list of every Java String function because the String class in Java 17 (the current LTS) contains over 70 public methods, and new ones are added in each release. This article covers all the essential and commonly used String methods, grouped by purpose, with syntax examples and notes on behavior.

Why the String Class Is Special

In Java, String is not a primitive type but a class in java.lang. It is immutable – once created, a String object’s value cannot be changed. Every method that appears to modify a string (like replace or substring) actually returns a new String object. This immutability makes strings thread-safe and allows them to be cached in the string pool. Because strings are used so frequently, Java provides a rich set of methods to inspect, compare, search, and transform them.

Creating and Converting Strings

You can create a String using a literal or the new keyword, but literals are recommended for performance. Conversion methods let you turn other types into strings and vice versa.

  • String.valueOf(x) – static method that converts primitives, char arrays, or objects to a String. For example, String.valueOf(123) returns "123".
  • new String(char[]) or new String(byte[]) – creates a String from a character or byte array.
  • toCharArray() – returns a new char[] containing the string’s characters.
  • getBytes() – returns a byte array using the platform’s default charset (or you can specify one).
  • format(String format, Object... args) – static method that returns a formatted string using printf-style specifiers, e.g., String.format("Hello %s!", "World").
  • join(CharSequence delimiter, CharSequence... elements) – static method that joins multiple strings with a delimiter, e.g., String.join("-", "a", "b", "c") returns "a-b-c".

Comparing Strings

Comparing strings is a common task, but the == operator checks reference equality, not content. Use these methods:

  • equals(Object obj) – returns true if the content is exactly the same, case-sensitive.
  • equalsIgnoreCase(String another) – compares content ignoring case.
  • compareTo(String another) – lexicographic comparison; returns 0 if equal, negative if this string comes before the argument, positive if after.
  • compareToIgnoreCase(String another) – same as above but ignores case.
  • contentEquals(CharSequence cs) – compares content with a CharSequence (like StringBuilder) without requiring it to be a String.
  • regionMatches(...) – compares subregions of two strings.

Common mistake: Using == to compare strings. For example, new String("a") == "a" is false because they are different objects. Always use .equals() for content comparison.

Searching and Extracting Characters

These methods help you find characters or substrings and pull parts out of a string.

  • charAt(int index) – returns the character at the given index (0-based).
  • indexOf(int ch) or indexOf(String str) – returns the first index of the character/string, or -1 if not found.
  • lastIndexOf(...) – returns the last occurrence index.
  • contains(CharSequence s) – returns true if the string contains the specified sequence.
  • startsWith(String prefix) and endsWith(String suffix) – check the beginning/end of the string.
  • substring(int beginIndex) or substring(int begin, int end) – returns a new string that is a part of the original. The end index is exclusive.
  • split(String regex) – splits the string into an array of strings based on a regular expression. For example, "a,b,c".split(",") returns ["a", "b", "c"].

Example: "Hello World".substring(6) returns "World".

Modifying and Transforming Strings

Because strings are immutable, these methods return new strings with the changes applied.

  • toUpperCase() and toLowerCase() – convert all characters to upper/lower case (using default locale; use toUpperCase(Locale) for locale-specific behavior).
  • trim() – removes leading and trailing whitespace (characters with code point <= U+0020). For modern Unicode whitespace, use strip() (Java 11+).
  • strip(), stripLeading(), stripTrailing() – remove Unicode whitespace from both ends, start, or end.
  • replace(char oldChar, char newChar) – replaces all occurrences of a character.
  • replace(CharSequence target, CharSequence replacement) – replaces all literal substrings (not regex).
  • replaceAll(String regex, String replacement) – replaces all substrings that match a regular expression.
  • replaceFirst(String regex, String replacement) – replaces only the first match.
  • concat(String str) – concatenates the argument to the end; equivalent to + operator.
  • repeat(int count) – (Java 11+) returns a string repeated count times.
  • intern() – returns a canonical representation from the string pool; rarely needed in modern code.

Example: " Hello ".trim() returns "Hello", and "abc".repeat(2) returns "abcabc".

Checking String Properties

These methods test the string’s characteristics.

  • length() – returns the number of characters (UTF-16 code units).
  • isEmpty() – returns true if length() == 0.
  • isBlank() – (Java 11+) returns true if the string is empty or contains only whitespace.
  • matches(String regex) – returns true if the entire string matches the regular expression.
  • equalsIgnoreCase() – already covered, but also a property check.

Working with Characters and Code Points

For Unicode-aware processing, Java provides methods that handle code points (full Unicode characters) rather than UTF-16 code units.

  • codePointAt(int index) – returns the Unicode code point at the index.
  • codePointBefore(int index) – returns the code point before the index.
  • codePointCount(int beginIndex, int endIndex) – counts the number of Unicode code points in the range.
  • offsetByCodePoints(int index, int codePointOffset) – returns the index after moving by a number of code points.

These are useful for emojis or characters outside the Basic Multilingual Plane.

Frequently Asked Questions

What is the difference between == and .equals() for strings?

== compares object references (memory addresses), so two strings with the same content but different objects will be false. .equals() compares the actual character sequence. Always use .equals() (or equalsIgnoreCase) when checking string content.

Why is String immutable in Java?

Immutability provides security (strings are often used for sensitive data like passwords), thread safety (no concurrent modification issues), and performance (string pooling and caching of hash codes). It also allows safe reuse of strings in collections.

How do I reverse a string in Java?

There is no built-in reverse() method on String. You can use new StringBuilder(str).reverse().toString() or convert to a char array and swap characters. For example: String reversed = new StringBuilder("hello").reverse().toString();

What is the difference between trim() and strip()?

trim() only removes characters with code points <= U+0020 (space, tab, newline, etc.). strip() (Java 11+) removes all Unicode whitespace characters, including non-breaking spaces and other Unicode space characters. Prefer strip() for modern applications.

How do I split a string by a period or other regex special character?

Because split() takes a regex, you must escape special characters. For a period, use "\\." (double backslash in Java string). For example: "a.b.c".split("\\.") returns ["a", "b", "c"]. Alternatively, use Pattern.quote(".") to escape.

Can I modify a String after creation?

No, String objects are immutable. Any operation that appears to change the string returns a new String. If you need a mutable string, use StringBuilder or StringBuffer (the latter is thread-safe but slower).

Conclusion

This article has covered the most important methods of the Java String class, from creation and comparison to searching and transformation. For a complete list, always refer to the official Java API documentation for your Java version, as new methods are added over time (e.g., strip, repeat, and isBlank in Java 11). Master these core methods, and you’ll handle most string manipulation tasks efficiently.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.