Java 将字符串拆分为字符串[],使每个元素最多包含100个字符,并以空格结尾
我该怎么做?我试过这样的方法,但似乎再也做不到了Java 将字符串拆分为字符串[],使每个元素最多包含100个字符,并以空格结尾,java,arrays,string,split,character,Java,Arrays,String,Split,Character,我该怎么做?我试过这样的方法,但似乎再也做不到了 public void speak(String text) { String[] textArray = text.split(" "); System.out.println(text); StringBuilder builder = new StringBuilder(); int index = 0; while(builder.length() < 99) {
public void speak(String text)
{
String[] textArray = text.split(" ");
System.out.println(text);
StringBuilder builder = new StringBuilder();
int index = 0;
while(builder.length() < 99)
{
builder.append(textArray[index] + " ");
index++;
}
System.out.println(builder.toString());
}
public void speak(字符串文本)
{
字符串[]textArray=text.split(“”);
System.out.println(文本);
StringBuilder=新的StringBuilder();
int指数=0;
while(builder.length()<99)
{
append(textary[index]+“”);
索引++;
}
System.out.println(builder.toString());
}
编辑:
我需要每个字符串是一个只有100个字符长的单独字符串。例如,我需要数组或Arralist中的每个字符串
ArrayList<String> lines = new ArrayList<>();
int index = 0;
while(index < textArray.length()) {
StringBuilder builder = new StringBuilder();
if(textArray[index].length() >= 99) {
build.append(textArray[index]);
index++;
} else {
while(builder.length() < 99 - (textArray[index].length())) {
builder.append(textArray[index] + " ");
index++;
if(!(index < textArray.length()) {
break;
}
}
lines.add(builder.toString());
}
将处理长度超过99个字符(无空格)的单词的大小写(应该很少)。正如所写的,它将使这个单词在一行中完全连在一起,并且可能导致一行超过100个字符。如果您想要不同的行为,请查看这段代码
但是超过99个字符的单词可能非常罕见,这可能是一个完全没有问题的问题。对于包装文本,而不是关注空格,关注100个字符的限制。对于大多数文本,空格的出现频率远远超过每100个字符一次
public void speak(String text) {
while(text.length() > 100) {
int nextSpace = text.lastIndexOf(" ", 99);
if (nextSpace == -1) { //no spaces for 100 characters
System.out.println(text.substring(0, 100));//give as many characters as possible, then split anyway
text = text.substring(100);
continue;
}
System.out.println(text.substring(0, nextSpace));
text = text.substring(nextSpace + 1);
}
System.out.println(text);
}
如果使用正则表达式,则需要更少的代码:
Pattern p=Pattern.compile("[\\s\\S]{99}");
Matcher matcher = p.matcher(s);
ArrayList<String> list= new ArrayList<>();
int lastFound=0;
while (matcher.find()){
list.add(matcher.group()+" ");
lastFound = matcher.end();
}
list.add(s.substring(lastFound));
Pattern p=Pattern.compile(“[\\s\\s]{99}”);
匹配器匹配器=p.Matcher(s);
ArrayList=新建ArrayList();
int lastFound=0;
while(matcher.find()){
list.add(matcher.group()+);
lastFound=matcher.end();
}
添加(s.substring(lastFound));
如果你有别的意思,就评论吧!可能您需要在字符串中添加更多模式,包括制表符、新行和其他特殊字符 我知道这个答案太晚了,效率有点低,但我认为它做得很好
public class StringSplitter
{
private int maxCharactersPerLine = 100;
public List<string> GetTextLines(string text)
{
var result = new List<string>();
if(text.Length <= this.maxCharactersPerLine)
{
result.Add(text);
return result;
}
string words[] = text.Split(new[]{' '}, StringSplittingOptions.RemoveEmptyEntries);
//accumolator, describes a line of text up to maximum character length.
string temp = string.Empty;
//this is so that we don't append an empty space on each new line.
bool isBeginingWord = true;
foreach(var word in words)
{
//there is a possibility that a text line has more than maximum
//consecutive characters without having a space. This is edge case.
if(temp.Length > this.maxCharactersPerLine)
{
result.Add(temp);
continue;
}
//if adding the next word in the list will exceed the
//maximum characters per line of text
if((temp + " " + word).Length > this.maxCharactersPerLine)
{
result.Add(temp); //add the accumolator to the list.
temp = ""; //reset the accumolator
isBeginingWord= true; //reset beginning of word flag.
continue;
}
//adding the next word from the list results in accumolator still
//still shorter than the maximum characters per line of text.
if(isBeginingWord)
{
temp = word;
isBeginingWord = false;
continue;
}
temp = temp + " " + word;
}
return result;
}
}
公共类stringspliter
{
私有整数maxCharactersPerLine=100;
公共列表GetTextLines(字符串文本)
{
var result=新列表();
如果(text.Length你在做什么?嗯,也许我明白了。你的while
循环应该是内部循环。你需要一个外部循环来包含它,以遍历整个文本数组
。是的,每次我尝试时,我都会遇到一个边界外错误。你能举个简单的例子说明你会怎么做吗?所以,空间赌注在每个单词之间,如果一个单词的当前行超过100个字符,它应该开始一个新的行吗?@nhgrif你现在消失的answear是最准确的,但它继续给我一个边界外的异常。不幸的是,我刚刚取消了它。并且做了一些修复,应该解决一些问题。再试一次。我可能失败了投票支持混乱的for
循环,我很快纠正了它。它工作不正常;将停止打印其他元素,而是在总数大于100个字符后无限循环-1@Saposhiente修正了。根据提问者对另一个答案的评论,他想要某种数组中的各行。仍然以so无限循环me输入,现在变得过于复杂。请看我的答案。你能解释一下它是如何无限循环的吗?是内部循环还是外部循环?是什么使它无限循环?这只会给我一个数组列表,其中everyword是数组中自己的元素。@user2298680这就是你在另一篇文章中的评论所说的你想要的。如果你愿意的话字符串,replace result.add(…)with builder.append(…+“”)我不明白您的意思。您可以在代码中更清楚地说明什么吗?@user2298680此代码从TextArray中获取字符串,它是在空格处拆分文本的结果。它将这些字符串移动到一个ArrayList中,这就是您所说的。(如果这不是您想要的,请澄清并告诉我们您的预期输出。)在将它们移动到ArrayList中时,它会检查它们的长度是否为100个字符或更长。如果是,它会将它们拆分为每个长度为100个字符或更短的字符串。我希望ArrayList中的字符串长度尽可能接近100个字符。但字符串必须以自然空格结尾!不会产生所需的输出;请参阅对我的回答的评论。
public class StringSplitter
{
private int maxCharactersPerLine = 100;
public List<string> GetTextLines(string text)
{
var result = new List<string>();
if(text.Length <= this.maxCharactersPerLine)
{
result.Add(text);
return result;
}
string words[] = text.Split(new[]{' '}, StringSplittingOptions.RemoveEmptyEntries);
//accumolator, describes a line of text up to maximum character length.
string temp = string.Empty;
//this is so that we don't append an empty space on each new line.
bool isBeginingWord = true;
foreach(var word in words)
{
//there is a possibility that a text line has more than maximum
//consecutive characters without having a space. This is edge case.
if(temp.Length > this.maxCharactersPerLine)
{
result.Add(temp);
continue;
}
//if adding the next word in the list will exceed the
//maximum characters per line of text
if((temp + " " + word).Length > this.maxCharactersPerLine)
{
result.Add(temp); //add the accumolator to the list.
temp = ""; //reset the accumolator
isBeginingWord= true; //reset beginning of word flag.
continue;
}
//adding the next word from the list results in accumolator still
//still shorter than the maximum characters per line of text.
if(isBeginingWord)
{
temp = word;
isBeginingWord = false;
continue;
}
temp = temp + " " + word;
}
return result;
}
}