简体   繁体   English

如何在java中对齐换行符?

[英]How to align newline in java?

Is there any way to align the very long text? 有没有办法对齐很长​​的文本?

For example of this code: 例如这段代码:

System.out.println("\tHi this is a very long string to test the automated newline but I want to have the newline to be tab indented too.");

Instead of having below 而不是在下面

    Hi this is a very long string to test the automated newline but I want to 
have the newline to be tab indented too.

I want this 我要这个

    Hi this is a very long string to test the automated newline but I want to 
    have the newline to be tab indented too.

So, I want the newline to be aligned with the first sentence. 所以,我希望换行符与第一句话一致。 Is there any library exist out there or default String Java method? 是否存在任何库或默认的String Java方法?

在print语句中使用(“\\ t”+“嗨,这是换行符”)

Could you show us the code you're using? 你能告诉我们你正在使用的代码吗? You would probably want \\t on both lines if you want them both aligned and tabbed. 如果你想让它们都对齐和标签,你可能会想要两条线上的\\ t。

In short, no. 简而言之,没有。 The width of the console can vary from execution to execution, and Java simply doesn't make that information available. 控制台的宽度可能因执行而异,而Java根本不会提供该信息。 However, you can probably assume that the minimum is 80 characters and just wrap it manually at 80 characters. 但是,您可以假设最小值为80个字符,并将其手动包装为80个字符。 See this question . 看到这个问题

I don't believe there is a built-in method in the JDK for doing this. 我不相信JDK中有一个内置方法来执行此操作。

The best solution I'm aware of is to use WordUtils.wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) which is part of Apache Commons Lang . 我所知道的最好的解决方案是使用WordUtils.wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) ,这是Apache Commons Lang的一部分。

This method allows you to specify the number of columns at which to wrap as well as your newline character. 此方法允许您指定要换行的列数以及换行符。 You could use \\n\\t as your newline which should wrap the line and then tab indent the next line as you've asked. 您可以使用\\n\\t作为换行符,它应该换行,然后按照您的要求将换行符缩进到下一行。

Example: 例:

import org.apache.commons.lang3.text.WordUtils;

public class Demo
{
    public static void main(String[] args)
    {
        String longString = "\tThe quick brown fox jumps over the fence";
        System.out.println(WordUtils.wrap(longString, 10, "\n\t", false));
    }

} 

-- Edit -- - 编辑 -

The Commons Lang library might be too large to include with an Android application. Commons Lang库可能太大而无法包含Android应用程序。 However, the source code for WordUtils.wrap(...) is readily available so you could always create your own stripped-down version of WordUtils . 但是, WordUtils.wrap(...)的源代码随时可用,因此您始终可以创建自己的精简版WordUtils

There's no direct way of achieving word-wrapping through String API. 没有通过String API实现自动换行的直接方法。 You would have to come up with a utility method of your own. 您必须提出自己的实用方法。 Here's a simple implementation that wraps the String input at the specified lineSize . 这是一个简单的实现,它将String输入包装在指定的lineSize

void printWordWrapped(String string, int lineSize) {
    int len = (len = string.length()) > lineSize ? lineSize : len;
    do {
        System.out.printf("\t%s\n", string.substring(0, len).trim());
        string = string.substring(len);
        len = (len = string.length()) > lineSize ? lineSize : len;
    } while (len > 0);
}

printWordWrapped("Hi this is a short string.", 30);
System.out.println();
printWordWrapped("Hi this is a very long string to test " +
                 "the automated newline but I want to have " + 
                 "the newline to be tab indented too.", 30);

Output : 输出

    Hi this is a short string.

    Hi this is a very long string
    to test the automated newline
    but I want to have the newline
    to be tab indented too.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM