迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 多行字符串

作者:迹忆客 最近更新:2023/03/26 浏览次数:

我们旨在探索适用于 Java 多行字符串的不同方法。我们还将看到哪种方法适合字符串数组。


Java 多行字符串的各种方法

我们有不同的方法来实现在 java 中编写多行字符串的目标。所有这些都在下面列出,你可以根据你的项目需求使用它们中的任何一个。

  1. 使用文本块(三个双引号""")。
  2. 使用+ 号。
  3. 使用 getProperty()concat() 方法。
  4. 使用 String 类的 format() 方法。
  5. 使用 String 类的 join() 方法。
  6. 对字符串数组使用 StringBuilder()

在 Java 中对多行字符串使用 Text Blocks

示例代码:

public class MainClass {
    public static void main(String args[]) {

        String str = """
        This is line one.
        This is line two.
        This is line three.
        This is line four.
        This is line five.
        """;

        System.out.println(str);
   }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

Java 15 带来了一种使用 Text Blocks 编写多行字符串的新方法,如果启用了预览功能,我们也可以在 Java 13 和 14 中使用它。使用 Text Blocks 的主要优点是我们不需要指定转义序列和连接运算符。

它还读取空格并将它们添加到输出中。你可能会发现在插入代码块时使用 Text Blocks 很有用,因为它通过读取空格来缩进标签。

示例代码:

public class MainClass {
    public static void main(String args[]) {

        String str = """
        <html>
            <head>
                <title> Learning Java Multiline Strings </title>
            </head>
            <body>
                <h1> Java Multiline String </h1>
            </body>
        </html>
        """;

        System.out.println(str);
    }
}

输出:

<html>
    <head>
        <title> Learning Java Multiline Strings </title>
    </head>
    <body>
        <h1> Java Multiline String </h1>
    </body>
</html>

在 Java 中使用 + 表示多行字符串

示例代码:

public class MainClass {
    public static void main(String args[]) {
        String str = "This is line one. \n" +
                     "This is line two. \n" +
                     "This is line three. \n" +
                     "This is line four. \n" +
                     "This is line five. \n";
        System.out.println(str);
  }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

我们编写多个字符串(每行一个字符串)并用 + 符号连接它们。在关闭每个字符串之前,我们还使用 \n 指定一个新行。

如果我们在多行上键入字符串但忘记写\n(用于新行),输出将如下所示。

输出:

This is line one.This is line two.This is line three.This is line four.This is line five.

在 Java 中对多行字符串使用 getProperty()concat() 函数

示例代码:

public class MainClass {
    public static void main(String args[]) {

        String newLine = System.getProperty("line.separator");
        String str = "This is line one."
                     .concat(newLine)
                     .concat("This is line two.")
                     .concat(newLine)
                     .concat("This is line three.")
                     .concat(newLine)
                     .concat("This is line four.")
                     .concat(newLine)
                     .concat("This is line five.");

        System.out.println(str);
    }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

我们可以使用 java.lang.System 类的方法 getProperty() 来获取 Java 编程中的行分隔符。getProperty() 方法获取属性的键并返回系统属性,由给定键表示(作为参数传递)。

此外,我们使用 concat() 方法将一个字符串附加到另一个字符串的末尾。在这里,它将新行与第一个字符串连接起来,然后将第二个字符串与新行连接起来,依此类推。


在 Java 中使用 String 类的 format() 方法处理多行字符串

示例代码:

public class MainClass {
    public static void main(String args[]) {
        String str = String.format("%s\n%s\n%s\n%s\n%s\n",
                                 "This is line one.",
                                 "This is line two.",
                                 "This is line three.",
                                 "This is line four.",
                                 "This is line five.");
        System.out.println(str);
    }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

在这种情况下,我们使用 format() 方法来格式化指定的字符串。虽然它工作得很好,但我们不推荐这个功能,因为它很难管理和保持%s\n 的计数。


在 Java 中使用 String 类的 join() 方法处理多行字符串

示例代码:

public class MainClass {
    public static void main(String args[]) {
        String str = String.join("\n",
                                 "This is line one.",
                                 "This is line two.",
                                 "This is line three.",
                                 "This is line four.",
                                 "This is line five.");
        System.out.println(str);
    }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

join() 方法似乎比 format() 方法更干净。join() 方法使用给定的分隔符连接给定的字符串。

我们使用 \n 作为本教程的分隔符。你可以使用逗号、句号或其他任何你想要的东西。


使用 StringBuilder 类的 append() 方法用于 Java 中的多行字符串

示例代码:

public class MainClass {
    public static void main(String args[]) {

        String newLine = System.getProperty("line.separator");
        StringBuilder string = new StringBuilder();

        String array[] = {
            "This is line one.",
            "This is line two.",
            "This is line three.",
            "This is line four.",
            "This is line five."
        };

        for(int i=0 ; i < array.length ; i++){
            string.append(array[i]);
            string.append(newLine);
        }

        System.out.println(string.toString());
    }
}

输出:

This is line one.
This is line two.
This is line three.
This is line four.
This is line five.

如果我们使用字符串数组,StringBuilder 类非常有用。它用于可修改(可变)的字符串。

然后,我们使用 append() 方法来追加驻留在数组中的字符串。最后,我们使用将任何对象转换为字符串的 toString() 方法在屏幕上打印字符串。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便