Java 将 StringBuffer 分成更小的部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9696429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dividing a StringBuffer into smaller parts
提问by user1092042
I have a large stringbuffer which i would like to break into smaller parts. The string buffer looks like this
我有一个很大的字符串缓冲区,我想把它分成更小的部分。字符串缓冲区看起来像这样
"name1+name2+name3+name4+..........+name2000"
Where
在哪里
name1=john
name2=prince
and so on.
(You get the idea.name1,name2,name3 stand for actual names of varying length)
(你明白了。name1,name2,name3 代表不同长度的实际名称)
Now i would like to store the names in a string array with each positon containing a 200 names.
现在我想将名称存储在一个字符串数组中,每个位置包含 200 个名称。
string[0]="name1+name2+name3+........+name200";
string[1]="name201+name202+...."
How would i go about achieving this task?
我将如何去完成这项任务?
采纳答案by noMAD
StringTokenizer str = new StringTokenizer(<StringBufferObject>);
int count = 0;
int arrCount = 0;
StringBuffer temp;
String[] stringArr = new String[x];
while(str.hasMoreTokens()) {
count++;
if(count != 200) {
temp.append(str.nextToken());
}
else {
stringArr[arrCount] = temp;
temp.delete(0,temp.length());
count = 0;
arrCount++;
}
回答by kundan bora
You must have some delimiter between each name. To break the string we should have some delimiter. If you have delimiter you can use subString() in for loop.
每个名称之间必须有一些分隔符。为了打破字符串,我们应该有一些分隔符。如果你有分隔符,你可以在 for 循环中使用 subString() 。
回答by Muhannad A.Alhariri
try to use
尝试使用
String[] tempNames = new String(namesBuffer).split("+");
and then
进而
int length = (tempNames.length / 200)+ (tempName.length % 200)
String[] names = new String[length];
for(int i = 0 ; i< tempNames.length ; i++){
for(int j = 0 ; j < length ; j++)
names[j] = tempNames[i];
}
hope this helps
希望这可以帮助
回答by Nishant
- Split on "+", using
String.split("\\+")
- Get chucks, in smaller arrays,
Arrays.copyOfRange(...)
- Join using Guava Joinerlike
Joiner.on("+").join(smallerArray)
- 在“+”上拆分,使用
String.split("\\+")
- 在较小的阵列中获取夹头,
Arrays.copyOfRange(...)
- 使用Guava Joiner加入,例如
Joiner.on("+").join(smallerArray)
回答by jahroy
It would be a lot easier to split a String using String.split() if that's possible:
如果可能的话,使用 String.split() 拆分字符串会容易得多:
/* something like this */
String arrayOfStrings = inputString.split("\+");
If you have to keep it as a StringBuffer you'll have to loop over the input and tokenize it yourself.
如果必须将其保留为 StringBuffer,则必须循环输入并自己标记它。
I guess it would look something like this:
我想它看起来像这样:
public String[] getTwoHundredStrings(StringBuffer inputBuff, String someToken)
{
String [] nameArray = new String [200];
int currentPos = 0;
int nextPos = 0;
for ( int i = 0; i < 200; i ++ ) {
nextPos = inputBuff.indexOf(someToken, currentPos);
if ( nextPos < 0 ) {
break;
}
String nextName = inputBuff.substring(currentPos, nextPos);
nameArray[i] = nextName;
currentPos = nextPos;
}
/* do some cleanup if nameArray has less than 200 elements */
return nameArray;