
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the easiest way to reverse a String in Java?
Built-in reverse() method
The StringBuffer class provides you a method named reverse(). It reverses the contents of the current StringBuffer object and returns the resultant StringBuffer object. It is the easiest way to reverse a Sting using Java. To do so −
Instantiate the StringBuffer class by passing the required String as a parameter.
Invoke the reverse() method od the created object.
Convert it into String again using the toString() method.
Example
public class Sample { public static void main(String args[]) { String str = new String("Hello how are you"); StringBuffer sb = new StringBuffer(str); String str2 = sb.reverse().toString(); System.out.println(str2); } }
Output
uoy era woh olleH
Let us observe two more ways to reverse a String
Using recursion
Recursion is a process of calling a function within itself, following java program reverses a Sting using recursion −
Example
public class StringReverse { public String reverseString(String str) { if(str.isEmpty()) { return str; }else { return reverseString(str.substring(1))+str.charAt(0); } } public static void main(String[] args) { StringReverse obj = new StringReverse(); String result = obj.reverseString("Tutorialspoint"); System.out.println(result); } }
Output
tniopslairotuT
Using toCharArray()
You can also convert the String to a character array and swap the characters of the array.
To reverse an array, swap the first element with the last element and the second element with second last element and so on, if the array is of odd length leave the middle element as it is.
If i is the first element of the array (length of the array –i-1) will be the last element therefore, swap array[i] with array[(length of the array –i-1)] from the start to the midpoint of the array −
Example
import java.util.Arrays; public class StringReverse { public static void main(String[] args) { String str = "Tutorialspoint"; char[] myArray = str.toCharArray(); int size = myArray.length; for (int i = 0; i < size / 2; i++) { char temp = myArray[i]; myArray[i] = myArray[size - 1 - i]; myArray[size - 1 - i] = temp; } System.out.println("Array after reverse:: "); System.out.println(Arrays.toString(myArray)); } }
Output
Array after reverse:: [t, n, i, o, p, s, l, a, i, r, o, t, u, T]
- Related Articles
- What is the easiest way to convert int to string in C++
- What is the easiest way to convert a List to a Set in Java?
- What is the easiest way to store date in MySQL database?
- What is the easiest way to lose weight without exercise?
- What is the easiest way to transfer files from phone to PC?
- What is the easiest way to initialize a std::vector with hardcoded elements in C++?
- The easiest way to concatenate two arrays in PHP?
- The easiest way to insert date records in MySQL?
- Easiest way to sort an array in MongoDB
- What is the correct way to check if String is empty in Java?
- Easiest way to get number of rows in a MySQL table?
- How to express the number16000 in exponential form in the easiest way?
- Java Program to Reverse a String
- The correct way to trim a string in Java.
- How to reverse a given string in Java?
