2 Best Ways To Reverse A String In Java

Sharing is healthy !!

In java a string can be reversed in ‘n’ number of ways. Here we learn 2 best ways to reverse a string in java.

Below two examples are the simplest way of reversing a string. Lets see its code implementation,

example: 2 Best Ways To Reverse A String In Java

Also Read – Best Way To Reverse String Array In Java

public class Reverse_String {

public static void main(String[] args) {
 
String str_Reverse = "Flower Brackets";
System.out.println("Before reverse - " + str_Reverse);
str_Reverse = new StringBuffer(str_Reverse).reverse().toString();
System.out.println("After reverse - " + str_Reverse);

}

}

Output:

2 Best Ways To Reverse A String In Java


Now using StringBuilder class,

example: 2 Best Ways To Reverse A String In Java

Also Read – String Compare Java Program

public class Reverse_String {

public static void main(String[] args) {
 
String str_Input = "Flower Brackets";
StringBuilder sb = new StringBuilder();
sb.append(str_Input);
sb = sb.reverse();
 
for(int x = 0;x < sb.length();x++)
{
System.out.println(sb.charAt(x));
}

}

}

Output:

stekcarB rewolF

Sharing is healthy !!