当前位置: 首页 > 知识库问答 >
问题:

Java中的文本块(或多行字符串)功能是什么?

储法
2023-03-14

JavaSE13引入了文本块(或多行字符串)特性。它与现有的字符串表示有什么区别和相似之处?

共有1个答案

龙晟睿
2023-03-14

文本块是一种多行字符串文字,该功能提供了一种以可预测方式格式化字符串的干净方法,无需使用大多数转义序列。它以一个“”“(三个双引号)开头和结尾,例如。

public class Main {
    public static void main(String[] args) {
        String text = """
                <html>
                    <head>
                        <title>Hello World</title>
                    </head>
                    <body>
                        Java rocks!
                    </body>
                </html>""";

        System.out.println(text);
    }
}

输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>

使用传统的字符串表示,代码看起来像

public class Main {
    public static void main(String[] args) {
        String text = "<html>\n"
                + " <head>\n"
                + "     <title>Hello World</title>\n"
                + " </head>\n"
                + " <body>\n"
                + "     Java rocks!\n"
                + " </body>\n"
                + "</html>";

        System.out.println(text);
    }
}

另一个关键的区别是文本块以三个双引号字符开始,后面跟着行终止符,这与传统的字符串表示不同。它的意思是

>

文本块的内容不能跟在同一行的三个开头双引号后面。

String str = "Hello World!"; // The traditional string

String textBlock = """
        Hello World!
        """; // The text block

String notAllowed = """Hello World!"""; // Error

// The following code will fail compilation
String notAllowed2 ="""Hello
         World!
        """;

关于缩进的注释:

编译器将完整的文本块向左移动,然后保留指定的行间距。

String str1 = """
   Hello""";
^^^<-----These three whitespace characters will be retained

演示:

public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}

输出:

Hello
Hello

   World!

Java rocks!

它作为预览功能在JavaSE13和JavaSE14中仍然可用,并已通过JavaSE15标准化。与任何预览功能一样,Java SE 13和14必须使用--启用预览选项编译和执行,例如。

编译:

javac --enable-preview --release 13 Main.java

执行:

java --enable-preview Main

是的,他们是。文本块编译为与传统字符串值相同的类型,即字节码不区分传统字符串值和文本块。

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello World!";
        String str2 = """
                Hello World!""";
        System.out.println(str1 == str2);
    }
}

输出:

true

是的,文本块可以连接到传统的字符串值或其他文本块,连接方式与连接传统的string值相同。如上所述,字节码不区分传统的字符串值和文本块。

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello ";
        String str2 = """
                World!""";
        String str3 = """
                 Java rocks!
                """;
        System.out.println(str1 + str2);
        System.out.println(str1 + (str2 + str3));
    }
}

输出:

Hello World!
Hello World! Java rocks!

请注意,我在str1中的Hello后面放了空白,在之前放了另一个空白Java!在str3中。

是的,转义序列将以传统的方式进行评估。

public class Main {
    public static void main(String[] args) {
        String text = """
                <html>
                    <head>
                        <title>Hello World</title>
                    </head>
                    <body>
                        Java\n\t\trocks!
                    </body>
                </html>""";

        System.out.println(text);
    }
}

输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>

是的,您可以使用%s$

public class Main {
    public static void main(String[] args) {
        String text = """
                What is the distance between %s and %s?""";

        System.out.println(String.format(text, "earth", "moon"));
        System.out.println(String.format(text, "earth", "mercury"));

        // Alternative-1
        text = """
                What is the distance between $celestial1 and $celestial2?""";

        System.out.println(text.replace("$celestial1", "earth").replace("$celestial2", "moon"));

        // Alternative-2 using the deprecated String#formatted
        text = """
                What is the distance between %s and %s?""";
        System.out.println(text.formatted("earth", "moon"));
    }
}

输出:

What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?

 类似资料:
  • 问题内容: 我对Java中的StringPool感到困惑。我在阅读Java中的String一章时遇到了这个问题。用外行的术语,请帮助我了解StringPool的实际作用。 问题答案: 打印(即使我们不使用方法:比较字符串的正确方法) 当编译器优化你的字符串文字时,它会看到两者s和t具有相同的值,因此你只需要一个字符串对象。这是安全的,因为在中是不可变的。 结果,两者和都t指向同一个对象,节省了一些

  • 问题内容: 在Android API中http://developer.android.com/guide/topics/data/data- storage.html#pref 它说: 共享首选项允许您保存和检索原始数据类型的持久键-值对。您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长型和字符串。 字符串是原始数据类型还是对象? 问题答案: 就编程语言而言

  • 问题内容: 假设我有一个包含以下内容的txt文件: 用户将输入一个字符串,例如“ omar”。我希望程序在该txt文件中搜索字符串“ omar”,如果该字符串不存在,则仅显示“不存在”。 我尝试了String.endsWith()或String.startsWith()函数,但该函数显示3次“不存在”。 我仅在3周前才开始使用Java,所以我是个新手……请多多包涵。谢谢。 问题答案: 只需阅读此文

  • 问题内容: 我正在寻找一个散列函数: 很好地哈希 文本字符串 (例如,很少冲突) 用Java编写,并被广泛使用 奖励:适用于多个字段(而不是我将它们串联并在连接的字符串上应用哈希) 奖励:具有128位变量。 奖励:不占用CPU。 问题答案: 您为什么不使用默认值的变体(一些真正聪明的人肯定会努力使它变得高效- 更不用说已经看过此代码的数千名开发人员的眼睛了)? 如果您要查找更多位,则可以使用“ 编

  • 本文向大家介绍Java中的字符串常量池是什么?,包括了Java中的字符串常量池是什么?的使用技巧和注意事项,需要的朋友参考一下 当您将字符串存储为 JVM会直接在一个单独的内存块(称为String常量池)中创建具有给定值的String对象。 每当我们尝试创建另一个String作为 JVM会验证String常量池中是否存在具有相同值的String对象,如果不是,JVM会创建新对象而不是创建新对象,而

  • 问题内容: 下面的代码试图删除字符串中所有重复的字符。我不确定代码是否正确。有人可以帮助我处理代码吗(即,字符匹配时实际发生了什么)? 问题答案: 该功能对我来说很好。我已经写了内联评论。希望能帮助到你: