将字符串中的行限制为60个字符,并带换行符



该程序用于删除double, triple…字符串中的空格,每行字符限制为60个。

问题是,当它必须跳过它们时,它会将单词放到第60行中。

输出是:

Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems' Java platform.

但是正确的输出必须是:

Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.

我的代码:


StringBuilder sb = new StringBuilder(text.replaceAll("(^ )|( $)", "").replaceAll("\s{2,}", " "));
int i = 0;
while ((i = sb.indexOf(" ", i + 60)) != -1) {
sb.replace(i,i+1,"n");
}
String result = sb.toString();
return result;
}

这是我第24次测试的结果。

Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems'   Java platform.
Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.

第一段是输入。第二段是输出。

看一下代码。您看到几个注释掉的System.out.println语句了吗?这就是发现代码问题的方法。您编写几行代码,然后打印中间结果。冲洗并重复,直到代码工作为止。

下面是完整的可运行代码。

public class FixedLengthLines {
public static void main(String[] args) {
new FixedLengthLines().processString();
}

public void processString() {
String text = "Java was originally developed by James Gosling at Sun Microsystemsrn" + 
"(which has since been acquired by Oracle) and released in 1995rn" + 
"as a core component of Sun Microsystems'   Java platform.";
String temp = text.replaceAll("\s{2,}", " ");
//      System.out.println(temp);

int lineLength = 60;
int lineIndex = 0;
int endIndex = Math.min(lineIndex + lineLength, temp.length());
StringBuilder builder = new StringBuilder();

while (lineIndex < endIndex) {
//          System.out.println(lineIndex + " " + endIndex + " " + temp.length());

if (endIndex >= lineIndex + lineLength) {
endIndex = temp.lastIndexOf(" ", endIndex); 
}

builder.append(temp.substring(lineIndex, endIndex));
builder.append(System.lineSeparator());

//          System.out.println(endIndex + " " + temp.substring(lineIndex, endIndex));

lineIndex = endIndex + 1;
endIndex = Math.min(lineIndex + lineLength, temp.length());
}

System.out.println(text);
System.out.println();
System.out.println(builder.toString());
}
}

给你。在你替换之后。不是很有效,但应该能奏效。

int pos = 0;
StringBuilder stringBuilder = new StringBuilder(text);
String finalText = recursiveBuild(stringBuilder,  pos).toString();
System.out.println(finalText);
}
private static StringBuilder recursiveBuild(StringBuilder stringBuilder, int pos) {
if (pos+60<stringBuilder.length()){
if (pos == 0) pos+=60;
int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
stringBuilder.delete(tempPos, tempPos+1).insert(tempPos, "n");
stringBuilder = recursiveBuild(stringBuilder, tempPos+60);
} else {
int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
stringBuilder.delete(tempPos, tempPos+1).insert(tempPos, "n");
}
return stringBuilder;
}

仅适用于

String text = "Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform.";

希望你能找到一个全正则表达式的"几乎一个"答案有趣(注需要JDK9+)。这借用了@Gilbert Le Blanc答案中清理过的temp字符串,但附加了空格,以便每个单词以单个空格结束:

String text = "Java was originally developed by James Gosling at Sun Microsystemsrn" +
"(which has since been acquired by Oracle) and released in 1995rn" +
"as a core component of Sun Microsystems'   Java platform.";
System.out.format("%ntext:%n%s%n", text);
String temp = text.replaceAll("\s{1,}", " ").trim()+" ";
System.out.format("%ntemp:%n%s%n", temp);

当最后一个匹配的字母不是空格时,可以使用replaceAll(Function<MatchResult, String> replacer)的另一个正则表达式将完整的单词拆分为一行最多60个字符:

// Word wrap at 60th:
String word60 = Pattern.compile("(.{0,59}[^ ])[ $]").matcher(temp)
.replaceAll(mr -> mr.group().trim()+System.lineSeparator());
System.out.format("%nword60:%n%s%n", word60);

印刷:

text:
Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems'   Java platform.
temp:
Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform. 
word60:
Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.

最新更新