interview-----str----倒序输出

方法一:利用String类的toCharArray(),再倒序输出数组。
 public class javatest { 
    public static void main(String[] args) { 
       String originalString = "abcdefg"; 
       String resultString = ""; 
       char[] charArray = originalString.toCharArray(); 
       for (int i = charArray.length-1; i>=0; i--){ 
          resultString +=charArray[i]; 
       } 
       System.out.println(resultString); 
    } 
}

方法二:
如果使用reverse()方法
public class reverseTest { 
    
    public static void main(String[] args) { 
         String originalString = "abcdefg"; 
          StringBuffer stringBuffer = new StringBuffer(originalString); 
          System.out.println(stringBuffer.reverse()); 
   } 
}

你可能感兴趣的:(interview)