Java 演示字符串插值
字符串插值是一种将占位符字符与变量(或后续示例中的字符串)替换来动态或有效地显示输出文本的字符串连接技术。
字符串插值重构代码,避免了反复使用变量来生成输出的需求。通过将占位符替换为字符串分配给变量名称,可以方便地编写较长的变量名称或文本。
在Java中,有几种方法可以使用连接运算符和库函数或类来执行字符串插值。
在这里,我们将讨论四种不同的字符串插值方法。
- 通过‘+’操作符
-
通过format()方法
-
通过MessageFormat类
-
通过StringBuilder类
使用‘+’操作符
在双引号之外的语句中,我们可以使用+操作符对字符串进行操作。根据情况或上下文,变量或字符串名称应在+操作符之前或之后。通过将变量替换为其值,我们实现了文本的插值或连接。
示例
下面的程序演示了使用 +操作符进行字符串插值的用法。
在StringInterpolation1类中,声明了三个字符串变量str1、str2和str3,并分别赋予了值 – “让我们创造世界”,“生活”和“在。”
通过使用 +操作符 将它们连接在一起,最终字符串显示在屏幕上。一部分最终字符串也写在输出语句中,但这是可选的。
因此,它是一种易于使用的字符串插值方法。
// Java Program to Illustrate String Interpolationb By the means of the + Operator
import java.io.*;
public class StringInterpolation1 {
// Main method
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. ";
// display the Interpolated string
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 ";
// display the interpolated string
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类
我们必须导入MessageFormat类,该类提供了format函数,该函数与String类中的format方法几乎相似。在这个函数中,占位符使用索引编号如“0”,“1”,“2”等进行替换。由于它可以避免重复使用同一变量,因此与字符串类中的format方法相比,它具有一些积极的优势。
示例
以下程序是为了演示使用MessageFormat类进行字符串插值的用法。
// java program to illustrate string interpolation by the means of MessageFormat class
import java.io.*;
// Import MessageFormat class from the java.text package
import java.text.MessageFormat;
public class StringInterpolation2 {
// Main method
public static void main(String[] args) {
// assigning value in String 1
String str1 = "Have";
// String 2
String str2 = " working hard ";
// String 3
String str3 = "will";
// String 4
String str4 = "into";
// display the interpolated string
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类允许链式调用尽可能多的append过程。然而,这使得代码难以阅读。
示例
下面的程序是为了演示使用 StringBuilder类进行字符串插值的用法。
// Java Program to illustrate String Interpolation
// by the means of StringBuilder class
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";
//display the interpolated string through
//StringBuilder class and append() method
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”,“是一个网站,”和“在技术和非技术领域”初始化。 使用一个新的 StringBuilder 对象显示最终的字符串,该对象以str1的初始值创建。此外, StringBuilder 类的 append() 方法被调用四次,分别附加str2的值,一个字符串常量“ offers a vast range of topics”,str3和字符串末尾的句号。
结论
本文重点介绍了Java中的字符串插值的演示。文章从讨论字符串插值的术语开始。此外,讨论了四种方法及其实现,用于执行字符串的插值。