java - 具有点的多行自动换行

标签 java word-wrap

我们必须在 java 中进行自动换行。例如。考虑这些行(确切地说是它们的书写方式):

When the ship, mostly carrying people from north Andhra Pradesh and south Odisha, 
for labour work in the Andaman and Nicobar islands, 
A.  sailed from the Visakhapatnam harbour, none had any suspicion of the harrowing.
B.  The ship was stuck for several hours in the high seas about 14 nautical miles

Only on the intervention of senior officials and the Navy, the ship crew was advised to 
return to the port for undertaking repairs on one of 
the defunct generators noticed mid-sea.

我们必须遵守的规则:

  1. 行应包含 98 个字符
  2. 如果点像 A. 和 B. 一样出现,那么它们必须排在不同的行中 无论它们是否有 98 个字符。

我们尝试遵循规则并编写代码,但无法处理行尾。因为前端的用户没有输入文本。他正在从写字板复制文本并按照上面编写的相同方式粘贴它。

由于无法获取\n 或\r,如果用户在行尾按 Enter 键,理想情况下必须获取\n 或\r。

文本换行后,结果应如下所示:

When the ship, mostly carrying people from north Andhra Pradesh and south Odisha, for labour work
in the Andaman and Nicobar islands, 
A.  sailed from the Visakhapatnam harbour, none had any suspicion of the harrowing.
B.  The ship was stuck for several hours in the high seas about 14 nautical miles

Only on the intervention of senior officials and the Navy, the ship crew was advised to return to
the port for undertaking repairs on one of the defunct generators noticed mid-sea.

任何人都可以帮助我们吗?

最佳答案

我不会给你完整的答案,因为这听起来很像家庭作业。 SO 不会为你做作业。但我们确实提供帮助!

所以,基本上,您有一大块文本需要拆分。句子以点 (.) 结尾(除此之外,根据需要进行调整)。 如果在您的情况下,一个句子有两个字符长,其中一个是一个点(例如 A.),那么您认为它是一个项目符号/点。

要将文本 block 分成句子,只需在点上拆分即可。 final String[] Sentences = text.split("\\.");

现在您可以处理每个句子。由于您的项目符号位于逗号之后,因此您还需要按逗号拆分以获取句子的每个部分并检查该部分是否是项目符号。

这里有一些可以帮助您入门的内容。

private void splitText(){
    final String text = "When the ship, mostly carrying people from north Andhra Pradesh and south Odisha,    for labour work in the Andaman and Nicobar islands,    A.  sailed from the Visakhapatnam harbour, none had any suspicion of the harrowing.   B.  The ship was stuck for several hours in the high seas about 14 nautical miles    Only on the intervention of senior officials and the Navy, the ship crew was advised to    return to the port for undertaking repairs on one of    the defunct generators noticed mid-sea.";
    int maxLength = 98;
    final String pText = text.replaceAll("\\s", " ");
    final String[] sentences = pText.split("\\.");

    for(String sentence : sentences){
        String[] parts = sentence.split("\\,");
        int lineLength = 0;
        for(int p=0, partLength=parts.length; p<partLength; ++p){
            String part = parts[p];
            int length = part.length();
            int trimmedLength = part.trim().length();
            if(trimmedLength == 1){
                System.out.println();
            }
            for(int i=0; i<length; ++i){
                char c = part.charAt(i);
                System.out.print(c);
                ++lineLength;
                if(lineLength == maxLength){
                    System.out.println();
                }
            }
            if(p != partLength-1){ System.out.print(",");}
        }
    }
    System.out.print(".");
}

这样,您将得到以下输出

When the ship, mostly carrying people from north Andhra Pradesh and south Odisha,    for labour work
 in the Andaman and Nicobar islands
    A  sailed from the Visakhapatnam harbour, none had any suspicion of the harrowing
   B  The ship was stuck for several hours in the high seas about 14 nautical miles    Only on the int
ervention of senior officials and the Navy, the ship crew was advised to    return to the port for undertaking repairs on one of    the defunct generators noticed mid-sea.

这是故意的一个未完成的解决方案,因为它听起来很像家庭作业。然而,它应该会插入你朝着正确的方向前进,让某些事情发挥作用。

关于java - 具有点的多行自动换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796959/

相关文章:

java - 导出期间将完整的 jtextarea 内容显示到 Excel 文件的单个单元格中。

android - 有没有办法在 Android 的 ListView 中禁用换行?

java - 如何使用 Zk 在 Datebox 中禁用日期

java - 使用 SimpleMailMessage() 发送邮件时 Spring 中的 NullPointerException

java - 数据库异常 -> 在数据库类或 Controller 类中抛出异常

Javascript函数将文本分割成两个长度相同的字符串

css - 如何使段落中的图像 float

java - 上传文件到 FTP 服务器时出现异常

java - 通过 Java 中的另一个函数传递字典值时通过引用更新字典值

html - 无法将表单放入 Bootstrap 中的框中