Java String transform() Method

The String.transform() method in Java is used to apply a transformation function to a string and return the result. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. transform Method Syntax
  3. Examples
    • Transforming a String to Uppercase
    • Reversing a String
    • Custom Transformations
  4. Conclusion

Introduction

The String.transform() method is a member of the String class in Java, introduced in Java 12. It allows you to apply a transformation function to a string and return the result, providing a convenient way to perform various operations on strings using lambda expressions or method references.

transform Method Syntax

The syntax for the transform method is as follows:

public <R> R transform(Function<? super String, ? extends R> f)
  • f: The transformation function to apply to the string.

Examples

Transforming a String to Uppercase

The transform method can be used to convert a string to uppercase.

Example

import java.util.function.Function;

public class TransformExample {
    public static void main(String[] args) {
        String message = "hello, world!";

        String uppercasedMessage = message.transform(String::toUpperCase);

        System.out.println("Original message: " + message);
        System.out.println("Transformed message: " + uppercasedMessage);
    }
}

Output:

Original message: hello, world!
Transformed message: HELLO, WORLD!

Reversing a String

The transform method can be used to reverse a string by applying a custom transformation function.

Example

import java.util.function.Function;

public class TransformExample {
    public static void main(String[] args) {
        String message = "hello, world!";

        String reversedMessage = message.transform(str -> new StringBuilder(str).reverse().toString());

        System.out.println("Original message: " + message);
        System.out.println("Transformed message: " + reversedMessage);
    }
}

Output:

Original message: hello, world!
Transformed message: !dlrow ,olleh

Custom Transformations

The transform method allows for various custom transformations. For example, you can apply a transformation that removes all vowels from a string.

Example

import java.util.function.Function;

public class TransformExample {
    public static void main(String[] args) {
        String message = "hello, world!";

        String noVowelsMessage = message.transform(str -> str.replaceAll("[aeiou]", ""));

        System.out.println("Original message: " + message);
        System.out.println("Transformed message: " + noVowelsMessage);
    }
}

Output:

Original message: hello, world!
Transformed message: hll, wrld!

Transforming a String to an Integer

The transform method can be used to convert a string to another type, such as an integer.

Example

import java.util.function.Function;

public class TransformExample {
    public static void main(String[] args) {
        String message = "12345";

        Integer number = message.transform(Integer::parseInt);

        System.out.println("Original message: " + message);
        System.out.println("Transformed message: " + number);
    }
}

Output:

Original message: 12345
Transformed message: 12345

Conclusion

The String.transform() method in Java is used for applying transformations to strings. By understanding how to use this method, you can efficiently perform various operations on strings using lambda expressions or method references in your Java applications. Whether you are transforming a string to uppercase, reversing it, applying custom transformations, or converting it to another type, the transform method provides a reliable solution for these tasks.

Comments