Java 程序说明字符串插值
字符串插值是一种连接技术,它通过将占位符替换为变量(或后续示例中的字符串),动态或有效地显示输出文本。
字符串插值重组代码,避免重复使用变量来生成输出。只需将占位符替换为分配给字符串的变量名,就可以高效地编写冗长的变量名或文本。
在 Java 中有几种方法可以借助连接运算符和库函数或类执行字符串插值。
这里,我们将讨论四种不同的字符串插值方法。
通过'+'运算符
通过 format () 方法
通过 MessageFormat 类
通过 StringBuilder 类
使用'+'运算符
在双引号外的语句中,我们可以使用 + 运算符对字符串进行操作。根据情况或上下文,变量或字符串名称应该位于 + 运算符之前或之后。我们通过将变量替换为其值来实现文本的插值或连接。
示例
以下程序用于演示如何使用 + 运算符进行字符串插值。
创建一个名为 StringInterpolation1 的类,其中声明了三个字符串变量,即 str1、str2 和 str3,并分别分配了值 - "让我们创造世界"、"生活"和"在。"。
最终的字符串通过 + 运算符 连接起来显示在屏幕上。最终字符串的一部分也写在输出语句中,但这是可选的。
因此,这是一种易于使用的字符串插值方法。
// Java 程序说明字符串插值b 通过 + 运算符
import java.io.*;
public class StringInterpolation1 {
// Main 方法
public static void main(String[] args){
// String 1
String str1 = "Let us make the world";
// String 2
String str2 = " to live ";
// String 3
String str3 = " in. ";
// 显示插值字符串
System.out.println(
str1 + " a better place "
+ str2 + str3);
}
}
输出
Let us make the world a better place to live in.
使用 format () 方法
此方法通过将内容与表达式和变量名称分开,使文本保持紧凑,并易于用于短句或表达式。由于 format () 方法接收字符串作为第一个参数,变量作为第二个参数,因此按顺序使用占位符(字符串的 %s)来适应表达式后提供的变量的值。因此,参数将比字符串占位符多一个。
示例
以下程序用于演示使用 format () 方法的字符串插值的用法。
// Java Program to Illustrate the working of String Interpolation
// by the means of the format () method
public class StringInterpolation2 {
public static void main(String[] args){
// String 1
String str1 = "Let us make the world";
// String 2
String str2 = "to live ";
// 显示插值字符串
System.out.println(String.format("%s a better place %s in.", str1,str2));
}
}
输出
Let us make the world a better place to live in.
在上面的代码中,创建了一个名为 StringInterpolation2 的类,其中声明了两个字符串,即 str1 和 str2,并分别分配了值"让我们创造世界"和"生活"。
最终的字符串通过使用 format 方法显示,该方法带有两个参数 -
包含占位符的字符串
要插入的字符串值
此处,该方法包含两个占位符,它们分别由 str1 和 str2 中提供的字符串值替换。
使用 MessageFormat 类
我们必须在此方法中导入提供 format 函数的 MessageFormat 类。除了占位符的编写方式外,MessageFormat 类中的 format 函数和 String 类中的 format 方法几乎相似。在此函数中,占位符使用索引编写,如"0"、"1"、"2"等。由于它可以防止重复使用相同的变量,因此与字符串类中的 format 方法相比,这具有一些积极的优势。
示例
以下程序用于演示使用 MessageFormat 类进行字符串插值的用法。
// java 程序用于说明通过 MessageFormat 类进行字符串插值
import java.io.*;
// 从 java.text 包导入 MessageFormat 类
import java.text.MessageFormat;
public class StringInterpolation2 {
// Main 方法
public static void main(String[] args) {
// 在字符串 1 中分配值
String str1 = "Have";
// String 2
String str2 = " working hard ";
// String 3
String str3 = "will";
// String 4
String str4 = "into";
// 显示插值字符串
System.out.println(MessageFormat.format("{0} faith, keep {1} everything {2} fall {3} line.", str1, str2, str3, str4));
}
}
输出
Have faith, keep working hard everything will fall into line.
在上述程序中,声明了四个字符串变量,即 str1、str2、str3 和 str4,并分别用值"Have"、"working hard"、"will"和"into"初始化它们。
插入的字符串通过 MessageFormat.format() 方法显示,该方法带有两个参数 -
包含要插入值的占位符的字符串,
要插入的值。
在这种情况下,字符串包含四个占位符 {0}、{1}、{2} 和 {3},它们分别由 str1、str2、str3 和 str4 的值替换。四个占位符按顺序放置,并显示最终输出。
此方法被认为比上面讨论的两种方法更好。
使用 StringBuilder 类
由于其长度和使用率低,此策略相对不常见。我们使用 StringBuilder 类创建一个新对象,然后使用 append 函数在准备好的文本之前添加变量。StringBuilder 类允许根据需要链接尽可能多的附加过程。然而,它使代码难以阅读。
示例
以下程序用于演示使用 StringBuilder 类的字符串插值的用法。
// Java 程序说明字符串插值
// 通过 StringBuilder 类
import java.io.*;
public class StringInterpolation4 {
public static void main(String[] args) {
// String 1
String str1 = "Tutorials Point ";
// String 2
String str2 = "is a website that";
// String 3
String str3 = "in technical as well as non technical domain";
//通过 StringBuilder 类和 append() 方法显示插值字符串
System.out.println(
new StringBuilder(str1)
.append(str2)
.append(" offers vast range of topics ")
.append(str3)
.append("."));
}
}
输出
Tutorials Point is a website that offers vast range of topics in technical as well as non technical domain.
在此程序中,声明了三个字符串变量,即 str1、str2 和 str3,并分别使用值"Tutorials Point"、"is a website that"和"in technology as well as non technology domain"进行初始化。
最终字符串通过使用新的 StringBuilder 对象显示在屏幕上,该对象使用 str1 的初始值创建。此外,StringBuilder 类的 append () 方法被调用四次,分别将 str2、字符串文字"offers a vast range of Topics"、str3 和句号的值附加到字符串末尾。
结论
This article threw light on the demonstration of String Interpolation in Java. The article began with a discussion of the term String Interpolation. Further, four methods and their implementation were discussed to perform interpolation of the strings.
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

