1、递归反转

1 public static String reverseString(String x) {
2         if (x == null || x.length() < 2)
3             return x;
4         else
5             return reverseString(x.substring(1)) + x.charAt(0);
6}

 

2、jdk自带的方法

1 public static String reverse(String str){
2      return new StringBuilder(str).reverse().toString();
3}

3、使用charAt()方法

1 public static String reverse(String str){  
2         String c ="";
3         for (int i = str.length() - 1; i >= 0; i--) {  
4               
5             c+= str.charAt(i);  
6               
7         }  
8         return c;
9 }    

 

相关文章:

  • 2021-07-01
  • 2021-10-03
  • 2021-09-19
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-27
  • 2021-04-30
  • 2022-12-23
  • 2021-12-03
  • 2021-11-17
  • 2021-06-15
  • 2021-12-27
相关资源
相似解决方案