这是一道面试题。问用StringBuffer在不使用新的String变量的情况下如何将一句英文反转。
如come on a little every day---->day every little a on come
package com.lee.sstring;
public class TestStringBuffer {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("come on a little every day");
int len = buffer.length();
int lastIndex = len;
int index = len;
while((index = buffer.lastIndexOf(" ", index-1)) != -1){
buffer.append(buffer.substring(index, lastIndex));
lastIndex = index;
}
buffer.append(buffer.append(" ").substring(0, lastIndex));
System.out.println(buffer.substring(len));
}
}