What is the best way to reverse a string in Java?
1 min readJun 18, 2022
--
In this tutorial we will see which is the best approach to reverse a string in java.
package com.java.programs;
// Write a Java Program to reverse a string.
public class Reverse_A_String {
public static void main(String[] args) {
// 1st approach
Long startTime1 = System.currentTimeMillis();
String input1 = "Gain java knowledge";
String output1 = "";
for(int i = input1.length()-1; i >= 0 ;i--){
output1 = output1+input1.charAt(i);
}
System.out.println("input1 : " +input1)…