🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
transformMethod Syntax- Examples
- Transforming a String to Uppercase
- Reversing a String
- Custom Transformations
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment