4

Java在字符串中添加或打印换行符

 1 year ago
source link: https://www.jdon.com/62495
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Java在字符串中添加或打印换行符


换行符因操作系统而异。例如,Linux 将换行符视为 \n,有时称为换行符(LF)。
而在windows操作系统中,新行是通过“\r\n”组合来考虑的,这被称为回车换行(CRLF)。
可以在字符串的任意索引处或字符串末尾添加换行符转义序列,例如“\n”、“\r”和“\r\n”。

在 Unix 中,只有 \n 字符足以将字符串分成新行,但在 Windows 中,\r 后面是 \n 字符。因此,Windows 操作系统需要两个字符的转义序列。
在开发 Java 应用程序和使用新换行符时,您必须特别注意它们,因为它们严格遵循操作系统规则。

始终建议使用System.lineSeparator()方法使用与操作系统无关的新行。因此,现在您无需担心与操作系统相关的问题。

先看看:不同操作系统的方式:

public class AddNewLineExample1 {

    public static void main(String[] args) {

        String line1 = "Hello engeers";
        String line2 = "hope you are staying safe";

        String line3 = line1 + "\n" + line2;

        System.out.println("Mac or unix or linux newline with \\n");
        System.out.println(line3);
    }
}
public class AddNewLineExample2 {

    public static void main(String[] args) {

        // windows os
        String line1 = "Hello engeers";
        String line2 = "hope you are staying safe";

        String line3 = line1 + "\r\n" + line2;

        System.out.println("windwos print newline with \\r\\n");
        System.out.println(line3);
    }
}

使用旧 Mac OS 在使用“\r”在 java 中打印新行:

public class AddNewLineExample3 {

    public static void main(String[] args) {
        
        // old mac os based
        String line1 = "Hello engeers";
        String line2 = "hope you are staying safe";
        
        String line3 = line1 + "\r" + line2;
        
        System.out.println("Old mac os print newline with \\r");
        System.out.println(line3);
    }
}

为了确保您的代码在任何操作系统上始终正常运行,java 提供了一个系统 API 来独立于 java 代码添加新行。
这可以通过以下两种方式完成。

a) System.lineSeparator()

b) System.getProperty()

public class AddNewLineExample4 {

    public static void main(String[] args) {

        // Using java api system class lineSeparator() method
        String line1 = "Hello engeers";
        String line2 = "hope you are staying safe";

        String line3 = line1 + System.lineSeparator() + line2;

        System.out.println("print newline with System.lineSeparator() method");
        System.out.println(line3);
    }
}
public class AddNewLineExample4 {

    public static void main(String[] args) {

        // Using java api system class getProperty() method
        String line1 = "Hello engeers";
        String line2 = "hope you are staying safe";

        String line3 = line1 + System.getProperty("line.separator") + line2;

        System.out.println("print newline with System.getPropertyr() method");
        System.out.println(line3);
    }
}

使用独立于系统的"%n":

public class AddNewLineExample5 {

    public static void main(String[] args) {

        // Using system dependent %n
        String line1 = "Hello engeers%nwelcome to the java blog";

        System.out.println("print newline with %n line separator");
        System.out.printf(line1);
    }
}

如果您在 java 代码中构建 Html 标记,那么您有三个选项来添加新的空行。

3.1 Html Break标签

3.2 换行\n字符

3.3 Unicode 字符

ASCII 码 13 对应于回车(它是“\r”)。
ASCII 代码 10 对应于换行符(它是“\n”)。

public class AddNewLineExample6 {

    public static void main(String[] args) {

        // html break tag
        String tag1 = "<p>hello</p>";
        String tag2 = "<p>world</p>";

        String tag3 = tag1 + "</br>" + tag2;

        // using java \n
        String tag4 = tag1 + "\n" + tag3;

        // using unicodes

        String tag5 = "<p>This is paragraph text and 
 woops there is a new line.</p>";
    }
}

 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK