将String拆分为String ,以便每个元素最多100个字符并以空格结尾
我该怎么做呢? 我试过这样的事情,但我似乎无法再进一步了。
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()); }
编辑:我需要每个字符串是单独的字符串,只有100个字符长。 所以我需要一个Array或Arralist中的每个String。
ArrayList 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()); }
内部while
循环将执行它在代码中已经执行的操作,构建一行,长度小于100个字符,在每个单词后面添加一个空格。
然后,当我们超过100个字符时,在字符串中添加一个新的行字符,然后继续。
作为一个注释,这段代码:
if(textArray[index].length() >= 99) { build.append(textArray[index]); index++; }
将处理长度超过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); }
我知道这个答案很晚,而且效率有点低,但我认为它能很好地完成工作。
public class StringSplitter { private int maxCharactersPerLine = 100; public List GetTextLines(string text) { var result = new List (); 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; } }
是否可以使用正则表达式,它需要更少的代码:
Pattern p=Pattern.compile("[\\s\\S]{99}"); Matcher matcher = p.matcher(s); ArrayList list= new ArrayList<>(); int lastFound=0; while (matcher.find()){ list.add(matcher.group()+" "); lastFound = matcher.end(); } list.add(s.substring(lastFound));
如果您的意思是其他只是评论! 可能是你需要在你的字符串中添加更多模式,包括标签,新行和其他特殊字符。
- OCSP检入Java安全套接字
- Spring拦截器与servletfilter
- 获取除字符串中的最后一个单词之外的每个单词的最简单方法
- 将PNG转换为JPEG
- org.codehaus.jackson.map.JsonMappingException:无限递归(StackOverflowError)
- 如何减去常春藤依赖集
- Java:尝试编译Hadoop程序时找不到com.sun.tools.javac.Main
- 从EnitityManager获取所有映射的实体
- ClassFormatError:在类文件javax / mail / MessagingException中非本机或抽象的方法中的Absent Code属性