java.lang.StringBuffer.reverse();eg.
String str = "Doubt is the vestibule";
StringBuffer buffer = new StringBuffer(str);
buffer = buffer.reverse();
System.out.println(buffer.toString());
String str = "Doubt is the vestibule";
StringBuffer buffer = new StringBuffer(str);
buffer = buffer.reverse();
System.out.println(buffer.toString());
reverse()只能接字母将全部的反掉,而不能按词来反。
上面的输出应该为:
elubitsev eht si tbuoD
而不是:
vestibule the is Doubt要达到这个效果估计得自已来实作。但楼主为什么要得到这个奇怪的结果呢?
反向排序
输出
先正序写进
再倒序取出也可以用stack
Stack sta=new Stack();
sta.push("Apple");
sta.push("banana");
sta.push("Cherry");
輸出但有問題就是我不知怎樣用這個Method我在sun的網上看到一個Sample:StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
println(st.nextToken());
}Output:this
is
a
test因為這個Method我不知怎樣用
所以不知怎樣將一個一個生字放在String數組中可以教教小弟嗎?這是我朋友的家課謝謝
public class test {
static public void main(String[] args) throws Exception {
StringTokenizer st = new StringTokenizer("this is a test");
List l = new ArrayList();
int i = 0;
while (st.hasMoreTokens())
{
l.add( i++, st.nextToken());
}
for( int j = l.size(); j > 0 ; j -- )
{
System.out.println(l.get(j-1));
}
}
}
将字串分为this is a test四个部分
如果字串为this#is#a#test
则可以用
StringTokenizer("this#is#a#test","#")
来分成this is a test四个部分