Java程序演示字符串插值


字符串插值是一种连接技术,用于通过用变量(或后续示例中的字符串)替换占位符字符来动态或有效地显示输出文本。

它重构代码,避免了重复使用变量来生成输出的需要。它可以通过用分配给字符串的变量名替换占位符来有效地编写冗长的变量名或文本。

Java中的字符串插值

Java中有多种方法可以使用连接运算符和库函数或类来执行字符串插值。在这里,我们将讨论四种不同的字符串插值方法:

  • 使用“+”运算符

  • 使用format()方法

  • 使用MessageFormat类

  • 使用StringBuilder类

使用“+”运算符

在双引号之外的语句中,我们可以使用+运算符操作字符串。根据具体情况或上下文,变量或字符串名称应该出现在+运算符之前或之后。我们通过用变量的值替换变量来实现文本的插值或连接。

示例

以下程序演示了使用+运算符进行字符串插值的用法。

// Java Program to Illustrate String Interpolation 
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.

使用MessageFormat类

要使用提供format()方法MessageFormat类,我们必须导入它。除了占位符的写法之外,MessageFormat类中的format()方法和String类中的format()方法几乎相同。

但是,MessageFormat类的format()方法避免了重复使用同一个变量。占位符使用索引编写,例如“0”、“1”、“2”等等。这比String类中的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.

使用StringBuilder类

由于其长度和低使用率,这种策略相对不常见。我们使用StringBuilder类创建一个新对象,然后使用append()方法在准备好的文本之前添加变量。此类允许根据需要链接任意数量的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.

结论

本文演示了Java中的字符串插值。文章首先讨论了字符串插值的术语。此外,还讨论了四种执行字符串插值的方法及其实现。

更新于:2024年7月31日

827 次浏览

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.