Java String transform() Method

The String.transform() method in Java is used to apply a specified function to a string, transforming it into another object. This method is part of the String class and was introduced in Java 12. It provides a convenient way to perform complex transformations on a string using functional programming techniques.

Table of Contents

  1. Introduction
  2. transform Method Syntax
  3. Examples
    • Basic Usage
    • Chaining Transformations
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.transform() method is a member of the String class in Java. It allows you to apply a given function to a string, resulting in a new object. This method is useful for performing various transformations and operations on strings in a functional programming style.

transform() Method Syntax

The syntax for the transform method is as follows:

public <R> R transform(Function<? super String, ? extends R> function)
  • function: A function that takes a String as input and returns a result of type R.

Examples

Basic Usage

The transform method can be used to apply a function to a string and obtain the transformed result.

Example

import java.util.function.Function;

public class TransformExample {
    public static void main(String[] args) {
        String str = "Hello, World!";
        Integer length = str.transform(String::length);
        System.out.println("Original string: " + str);
        System.out.println("Length of string: " + length);
    }
}

Output:

Original string: Hello, World!
Length of string: 13

Chaining Transformations

The transform method can be used to chain multiple transformations together.

Example

import java.util.function.Function;

public class ChainingTransformationsExample {
    public static void main(String[] args) {
        String str = "hello, world!";
        String result = str.transform(String::toUpperCase)
                           .transform(s -> s.replace("WORLD", "JAVA"))
                           .transform(s -> s + "!!!");

        System.out.println("Original string: " + str);
        System.out.println("Transformed string: " + result);
    }
}

Output:

Original string: hello, world!
Transformed string: HELLO, JAVA!!!

Handling Edge Cases

Example: Transforming an Empty String

If the string is empty, the transform method will apply the function to the empty string and return the result.

import java.util.function.Function;

public class TransformEmptyExample {
    public static void main(String[] args) {
        String str = "";
        String result = str.transform(s -> s.isEmpty() ? "Empty String" : s);
        System.out.println("Original string: '" + str + "'");
        System.out.println("Transformed string: '" + result + "'");
    }
}

Output:

Original string: ''
Transformed string: 'Empty String'

Real-World Use Case

Example: Parsing and Transforming User Input

One common use case for transform is parsing and transforming user input.

import java.util.Scanner;
import java.util.function.Function;

public class ParseAndTransformInputExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        String input = scanner.nextLine();

        Integer result = input.transform(Integer::parseInt)
                              .transform(n -> n * 2);

        System.out.println("Original input: " + input);
        System.out.println("Transformed result: " + result);

        scanner.close();
    }
}

Output:

Enter a number: 5
Original input: 5
Transformed result: 10

In this example, the transform method is used to parse the user input into an integer and then double the value.

Conclusion

The String.transform() method in Java is a powerful and convenient tool for applying functions to strings and obtaining transformed results. It supports functional programming techniques, making it easy to perform complex transformations and operations on strings. By understanding and utilizing the transform method, you can efficiently manage string transformation tasks in your Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top