方法一:利用String類的toCharArray(),再倒序輸出數組。
Java代碼
1 public class Javatest {
2 public static void main(String[] args) {
3 String originalString = "abcdefg";
4 String resultString = "";
5 char[] charArray = originalString.toCharArray();
6 for (int i = charArray.length-1; i>=0; i--){
7 resultString +=charArray[i];
8 }
9 System.out.println(resultString);
10 }
11 }
方法二:如果使用reverse()方法
1 Java代碼
2 public class reverseTest {
3
4 public static void main(String[] args) {
5 String originalString = "abcdefg";
6 StringBuffer stringBuffer = new StringBuffer(originalString);
7 System.out.println(stringBuffer.reverse());
8 }
9 }