多行词包装有点

时间:2016-09-30 17:29:51

标签: java word-wrap

我们在java中做自动换行。例如。考虑线条(完全按照它们写的方式):

docker inspect

我们要遵循的规则:

  1. 行数应为98个字符
  2. 如果有点像A.和B.那么他们就会有不同的路线 他们是否是98个字符。
  3. 我们已经尝试遵循规则并编写代码但无法处理行尾。因为前端的用户没有输入文本。他正在从wordpad中复制文本并以与上面相同的方式粘贴它。

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

    包装文本后,结果应该是这样的:

    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 个答案:

答案 0 :(得分:1)

我不会给你完整的答案,因为这听起来非常像家庭作业。所以不会为你做功课。但我们会帮帮忙!

所以,基本上,你有一大堆想要拆分的文字。一个句子以点(.)结束(除其他外,根据需要进行调整)。 如果,在你的情况下,一个句子是两个字符长,一个是一个点(例如A.),那么你认为它是一个子弹/点。

要将文本blob转换为句子,只需将其拆分为点。 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.

这是故意一个未完成的解决方案,因为它听起来像家庭作业。然而,它应该会推动你朝着正确的方向努力。