Java 如何将长字符串拆分为段落

Java 如何将长字符串拆分为段落,java,Java,我有一个长度为1000-1500个字符的字符串。我想把它分成几段。我现在做的是: String tempDisplayText = "this is the long...... string of length 1000-2000 chars"; String displayText = null; if (tempDisplayText != null && tempDisplayText.length() > 400) { int firstFullS

我有一个长度为1000-1500个字符的字符串。我想把它分成几段。我现在做的是:

String tempDisplayText =
    "this is the long...... string of length 1000-2000 chars";
String displayText = null;
if (tempDisplayText != null && tempDisplayText.length() > 400) {
    int firstFullStopIndex = tempDisplayText.indexOf(". ", 350);
    if (firstFullStopIndex > 0) {
        displayText = "<p>"
                + tempDisplayText.substring(0, firstFullStopIndex)
                + ".</p><p>"
                + tempDisplayText.substring(firstFullStopIndex + 1)
                + "</p>";
        feed.setDisplayText(displayText);
    }
}
String tempDisplayText=
“这是长度为1000-2000个字符的长……字符串”;
字符串displayText=null;
if(tempDisplayText!=null&&tempDisplayText.length()>400){
int firstFullStopIndex=tempDisplayText.indexOf(“.”,350);
如果(firstFullStopIndex>0){
displayText=“”
+tempDisplayText.substring(0,firstFullStopIndex)
+“

” +tempDisplayText.substring(firstFullStopIndex+1) +“

”; feed.setDisplayText(displayText); } }

这段代码运行良好,但只将整个字符串分为两段。但有时下一段太长,因此失去了可读性。在Java中,有没有标准的方法将字符串划分为段落?

我看不出有什么理由不在剩下的部分(即第二段)重复这一点。你不能分裂一个句子

StringBuilder sb = new StringBuilder();
if (tempDisplayText != null) {
    int firstFullStopIndex;
    while( tempDisplayText.length() > 400
       && 
       (firstFullStopIndex = tempDisplayText.indexOf(". ", 350)) >= 0 ){
    sb.append( "<p>" );
    sb.append( tempDisplayText.substring(0, firstFullStopIndex) );
    sb.append( ".</p>" );
    tempDisplayText = tempDisplayText.substring(firstFullStopIndex + 1);
    }
    if( tempDisplayText.length() > 0 ){
        sb.append( "<p>" ).append( tempDisplayText ).append( "</p>" );
    }
    tempDisplayText = sb.toString();
}
StringBuilder sb=新建StringBuilder();
if(tempDisplayText!=null){
int firstfullstop索引;
而(tempDisplayText.length()>400
&& 
(firstFullStopIndex=tempDisplayText.indexOf(“.”,350))>=0){
sb.追加(“”);
sb.append(tempDisplayText.substring(0,firstFullStopIndex));
sb.追加(“.

”); tempDisplayText=tempDisplayText.substring(firstFullStopIndex+1); } 如果(tempDisplayText.length()>0){ sb.append(“”).append(临时显示文本)。append(“

”); } tempDisplayText=sb.toString(); }
非常感谢,这确实有效。。我只做了一个小小的改变:最后一个。被取代,否则我会。。在documnet的结尾处。感谢您提供了一个lotSorry-copy粘贴,与往常一样:-\