我只能使用字符串来做这件事,例如:
String str="";
for(int i=0;i<100;i++){
str=i+str;
}
有没有办法通过StringBuilder来实现这一点呢?谢谢。
发布于 2011-05-09 08:11:40
您可以使用strbuilder.insert(0,i);
发布于 2011-05-09 12:27:38
也许我漏掉了什么,但是你想要得到一个像这样的字符串,"999897969594...543210"
,对吗?
StringBuilder sb = new StringBuilder();
for(int i=99;i>=0;i--){
sb.append(String.valueOf(i));
}
发布于 2015-01-08 15:13:02
作为另一种解决方案,您可以使用后进先出结构(如堆栈)来存储所有字符串,完成后,只需将它们全部取出并放入StringBuilder。它自然地颠倒了放置在其中的项(字符串)的顺序。
Stack<String> textStack = new Stack<String>();
// push the strings to the stack
while(!isReadingTextDone()) {
String text = readText();
textStack.push(text);
}
// pop the strings and add to the text builder
String builder = new StringBuilder();
while (!textStack.empty()) {
builder.append(textStack.pop());
}
// get the final string
String finalText = builder.toString();
https://stackoverflow.com/questions/5931261
复制相似问题