Easy way to reverse String

后端 未结 12 1043
野性不改
野性不改 2021-02-13 04:01

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

12条回答
  •  情歌与酒
    2021-02-13 04:26

    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....

提交回复
热议问题