说明字符串插值的Java程序
字符串插值是一个过程,其中占位符字符被替换为允许动态或有效地打印文本输出的变量(或在这种情况下的字符串)。字符串插值使代码更紧凑,避免重复使用变量打印输出。字符串插值用指定给字符串的上述变量名替换占位符,因此可以高效地编写大变量名或文本。
Java中的字符串插值可以通过一些连接运算符或内置函数或类以多种方式完成。
方法:
Java中进行字符串插值的一些方法如下:
- 使用“+”运算符
- 使用 format()函数
- 使用 MessageFormat 类
- 使用 StringBuilder 类
让我们单独讨论它们到一定深度
方法 1:使用“+”运算符
我们可以在双引号外的表达式中使用 +运算符来处理字符串。根据条件或场景,变量或字符串名称应在 +运算符之前或之后使用。该变量将被替换为其值,因此我们实现了字符串的插值或串联。
例子
Java
// Java Program to Illustrate String Interpolation
// Using the + Operator
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Input string
// String 1
String name = "Geeks for Geeks";
// String 2
String field = "coding";
// Print and display the string Interpolated
System.out.println(
name + " is the best platform to learn "
+ field);
}
}
Java
// Java Profram o Illustrate String Interpolation
// Using the format() method
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input strings
// String 1
String name = "Geeks for Geeks";
// String 2
String field = "Data Structures";
// Print and ddispalt the interpolated string
System.out.println(String.format(
"%s is the best platform to learn %s .", name,
field));
}
}
Java
// Java Program to Illustrate String Interpolation
// Using MessageFormat class
// Importing input output class
import java.io.*;
// Importing MessageFormat class from java.text package
import java.text.MessageFormat;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input strings
// String 1
String a = "Democracy";
// String 2
String b = "people";
// Print and display the interpolated string
System.out.println(MessageFormat.format(
"{0} is a government of the {1}, for the {1} and by the {1}.",
a, b));
}
}
Java
// Java Program to illustrate String Interpolation
// Using StringBuilder class
// Importing input output libraries
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom inout strings
// String 1
String a = "Geeks for Geeks";
// String 2
String b = "Data Structure and Algorithms";
// Print an display the interpolated string
// using the StringBuilder class and append() method
System.out.println(
new StringBuilder(a)
.append(" is the best portal to learn ")
.append(b)
.append("."));
}
}
Geeks for Geeks is the best platform to learn coding
输出说明:
在以下代码示例中,通过插入变量名称,在要打印的实际文本之间使用 +运算符插入或连接字符串变量名称和字段。这是连接字符串的最简单方便的方法之一,因为它可以根据要求进行自定义。也可以在没有引号的文本的情况下使用运算符。
方法二:使用format()函数
此方法将文本与表达式和变量名称分开,使其紧凑且易于用于小句子或表达式。占位符(字符串的 %s )按顺序用于适应表达式末尾提供的变量的值,因为 format()函数接受字符串作为第一个参数,然后是变量。因此参数的数量将比字符串中的占位符多 1。
例子
Java
// Java Profram o Illustrate String Interpolation
// Using the format() method
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input strings
// String 1
String name = "Geeks for Geeks";
// String 2
String field = "Data Structures";
// Print and ddispalt the interpolated string
System.out.println(String.format(
"%s is the best platform to learn %s .", name,
field));
}
}
Geeks for Geeks is the best platform to learn Data Structures .
输出说明:
在上面的代码示例中,我们使用函数format() 通过使用 %s运算符来排列字符串,该运算符用作字符串的占位符。必须在将替换占位符的打印字符串末尾添加顺序字符串。
方法 3:使用 MessageFormat 类
在这个方法中,我们必须导入 MessgeFormat 类,它为我们提供了格式函数。 MessageFormat 类中的格式函数几乎与 String 类中的格式函数相同,只是占位符的编写方式不同。此函数中的占位符是使用 {0}、{1}、{2}.. 等索引编写的。与字符串类中的 format函数相比,这可以有一些很好的优势,因为它可以避免一次又一次地重复使用相同的变量。
例子
Java
// Java Program to Illustrate String Interpolation
// Using MessageFormat class
// Importing input output class
import java.io.*;
// Importing MessageFormat class from java.text package
import java.text.MessageFormat;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input strings
// String 1
String a = "Democracy";
// String 2
String b = "people";
// Print and display the interpolated string
System.out.println(MessageFormat.format(
"{0} is a government of the {1}, for the {1} and by the {1}.",
a, b));
}
}
Democracy is a government of the people, for the people and by the people.
输出说明:
在上面的代码示例中,很明显 MessageFormat 类中的 format函数与 String 类函数相比是相当有效的。我们已经使用了变量 b 三次,只写了三次占位符,而不是 o 在特定情况下三次或多次写整个变量名。此函数在大文本打印或复杂系统中非常有效。
方法四:使用 StringBuilder 类
由于相同的原因,此方法相当冗长且不常用。我们使用 StringBuilder 类来实例化一个新对象,并调用 append函数在 append函数中的格式化文本之前添加变量。在 StringBuilder 类中链接的函数可能多达 append 函数。尽管它使代码的可读性迷失了方向。
例子
Java
// Java Program to illustrate String Interpolation
// Using StringBuilder class
// Importing input output libraries
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom inout strings
// String 1
String a = "Geeks for Geeks";
// String 2
String b = "Data Structure and Algorithms";
// Print an display the interpolated string
// using the StringBuilder class and append() method
System.out.println(
new StringBuilder(a)
.append(" is the best portal to learn ")
.append(b)
.append("."));
}
}
Geeks for Geeks is the best portal to learn Data Structure and Algorithms.
输出说明:
在上面的代码中, StringBuilder 可以选择接受参数作为变量,在这种情况下是字符串,然后我们可以链接 append函数来用所需的文本插入字符串。
Conclusion: Thus, from the above 4 methods, the best one will be dependent on the balance between scalability and readability of the code. There might be other methods out there, be sure to explore them. The above methods are the most common ones, so you can get up and running with minimal code and an understanding of the syntax.