Java String transform() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Java 12+

Definition

In Java, transform(f) calls the function f with the string as its argument and returns the result: s.transform(f) is the same as f.apply(s). The result can be any type, not just String.

Its value is readability: you can apply your own helper methods in the same left-to-right chain as built-in methods, instead of wrapping calls inside each other. The parameter type is Function<? super String, ? extends R>, so lambdas and method references both fit.

transform() is an instance method of java.lang.String, added in Java 12. Call it on a string, as in text.transform(...); calling it on a null reference throws NullPointerException. Requires Java 12 or newer.

Syntax

Pass a lambda or a method reference that accepts a String.

java
String clean = raw.transform(String::strip);
int number   = "42".transform(Integer::parseInt);
CallWhat it does
"ab".transform(s -> s + s)"abab".
"5".transform(Integer::parseInt)The Integer 5.
"hi".transform(String::length)2.

transform itself does nothing to the string; all behavior comes from the function you pass. If the function returns a String, you can keep chaining other String methods afterwards.

Library declarations
java
public <R> R transform(Function<? super String, ? extends R> f);

Parameters

ParameterDescription
fThe function to apply to this string.

Return Value

Return ValueDescription
RWhatever f returns (a String, a number, a list, ...).

Java Examples

Example 1 — Chain Clean-Up Steps Left to Right

Strip the input, then uppercase it, as a readable pipeline.

java
import java.util.Locale;

public class TransformChain {
    public static void main(String[] args) {
        String result = "  ada lovelace  "
                .transform(String::strip)
                .transform(s -> s.toUpperCase(Locale.ROOT));

        System.out.println("[" + result + "]");
    }
}

How It Works

  1. A method reference first. String::strip is a Function<String, String>, so transform applies it and returns the stripped text.
  2. A lambda next. The second step uppercases with Locale.ROOT; each step receives the previous result.
  3. Reads in order. The chain lists the steps in the order they run, instead of nesting f(g(x)).

Example 2 — Convert Text into Other Types

The result type follows the function: here an int and an Integer.

java
public class TransformParse {
    public static void main(String[] args) {
        int total = "42".transform(Integer::parseInt) + 8;
        Integer length = "hello".transform(String::length);

        System.out.println(total);
        System.out.println(length);
    }
}

How It Works

  1. String in, number out. Integer::parseInt turns "42" into 42, so R is Integer here.
  2. Use it like any number. The result is unboxed and adding 8 prints 50.
  3. Any String method works. String::length returns 5 for "hello"; generics make the result an Integer.

Example 3 — Build a Reusable Slug Function

Store a pipeline in a Function variable and apply it to any text.

java
import java.util.Locale;
import java.util.function.Function;

public class TransformSlug {
    public static void main(String[] args) {
        Function<String, String> slugify = s -> s.strip()
                .toLowerCase(Locale.ROOT)
                .replaceAll("[^a-z0-9]+", "-");

        System.out.println("  Hello, World! Java 12 ".transform(slugify));
        System.out.println("Ready Set Go".transform(slugify.andThen(String::length)));
    }
}

How It Works

  1. Define once. slugify strips, lowercases, and replaces every run of other chars with one dash.
  2. Apply anywhere. transform(slugify) turns the title into hello-world-java-12.
  3. Compose functions. andThen(String::length) builds a new function that slugifies and then measures; "ready-set-go" has 12 chars.

Frequently Asked Questions

It applies a Function to the string and returns the function result: s.transform(f) equals f.apply(s).
Java 12.
Yes. The return type R comes from the function, so it can return an Integer, a List, or any other type.
It keeps a chain of operations readable from left to right, mixing your own helpers with built-in String methods.
No. Strings are immutable; transform() just passes the string to your function.

Next: translateEscapes()

Turn escape sequences such as \n and \t into real characters (Java 15+).

translateEscapes() 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.

13 people found this page helpful