用java编写程序,使一段字符串输出时,与原串字符顺序相反
如果是字符串的话,可以通过以下方式逆序输出:
public class reverseTest {
public static void main(String[] args) {
String originalString = "abcdefg";
StringBuffer stringBuffer = new StringBuffer(originalString);
System.out.println(stringBuffer.reverse());
}
}
字符串的话,还可以通过以下方式逆序输出:
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);
}
}
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse().toString());
public class TestTemp {
public static void main(String[] args) {
String str = "ABC";
StringBuffer sbr = new StringBuffer();
for(int i=str.length()-1;i>=0;i--){
char c = str.charAt(i);
sbr.append(c);
}
System.out.println("result is :"+sbr);
}
}
慢了一步!