Reverse a string in Java

前端 未结 30 2629
礼貌的吻别
礼貌的吻别 2020-11-21 13:12

I have \"Hello World\" kept in a String variable named hi.

I need to print it, but reversed.

How can I do this? I understand there

30条回答
  •  灰色年华
    2020-11-21 13:58

    Procedure :

    We can use split() to split the string .Then use reverse loop and add the characters.


    Code snippet:

    class test
    {
      public static void main(String args[]) 
      {
          String str = "world";
          String[] split= str.split("");
    
          String revers = "";
          for (int i = split.length-1; i>=0; i--)
          {
            revers += split[i];
          }
          System.out.printf("%s", revers);
       }  
    }
    
     //output : dlrow
    

提交回复
热议问题