java string reverse function code example

Example 1: reverse string java

// Use StringBuilder for non-thread environment, it is faster
String string="whatever";
String reverse = new StringBuilder(string).reverse().toString();
System.out.println(reverse);

Example 2: how to reverse a string in java

public class ReverseString {
    public static void main(String[] args) {
        String s1 = "neelendra";
        for(int i=s1.length()-1;i>=0;i--)
            {
                System.out.print(s1.charAt(i));
            }
    }
}

Example 3: string reverse in java

String str = "Hello";
String reverse(String str){
  StringBuilder sb = new StringBuilder();
  sb.append(str);
  sb.reverse();
  return sb.toString();
}

Example 4: Reverse a string in java

// reverse a string using reverse() method of StringBuilder class
class ReverseUsingReverseMethod
{
   public static void main(String[] args)
   {
      String str = "Hello world Java";
      StringBuilder sb = new StringBuilder();
      // append string to StringBuilder
      sb.append(str);
      sb = sb.reverse();
      // printing reversed String
      System.out.println(sb);
   }
}