Easy way to reverse String

后端 未结 12 2504
北荒
北荒 2021-02-13 03:47

Without going through the char sequence is there any way to reverse String in Java

12条回答
  •  既然无缘
    2021-02-13 04:45

    Use StringBuilder's or StringBuffer's method... reverse()

     public class StringReverse
    {
      public static void main(String[] args)
      {
      String string=args[0];
      String reverse = new StringBuffer(string).reverse().toString();
      System.out.println("\nString before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
      } 
    } 
    

    StringBuffer is thread-safe, where as StringBuilder is Not thread safe..... StringBuilder was introduced from Java 1.5, as to do those operations faster which doesn't have any Concurrency to worry about....

提交回复
热议问题