在 Java 中格式化字符串
本篇文章介绍了如何在 Java 中进行格式化字符串,并列举了一些示例代码来理解这个主题。
字符串插值是将占位符替换为字符串文字中的值的过程。在 Java 中进行字符串插值,我们可以使用几种方法,如 String
类的 format()
方法、MessageFormat
类的 format()
方法、StringBuilder
类、String
类的 formatted()
方法等。
使用+
运算符的 Java 字符串插值(连词)
这是最简单的方法。我们可以使用+
来进行字符串插值。Java 使用+
操作符来连接变量与字符串。所以我们也可以用它来进行字符串插值。下面我们在字符串中放入两个变量,并得到一个字符串。
public class SimpleTesting{
public static void main(String[] args) {
String site_name = "Jiyik";
String type = "'How to Guide'";
String message = site_name+" is a "+ type + " Portal";
System.out.println(message);
}
}
输出:
Jiyik is a 'How to Guide' Portal
使用 format()
方法进行 Java 字符串插值
我们可以使用 String
类的 format()
方法,通过插值变量来格式化一个字符串。format()
方法需要两个参数,第一个是字符串格式,第二个是参数。
public class SimpleTesting{
public static void main(String[] args) {
String site_name = "Jiyik";
String type = "'How to Guide'";
String message = String.format("%s is a %s portal", site_name,type);
System.out.println(message);
}
}
输出:
Jiyik is a 'How to Guide' portal
使用 Java 中的 MessageFormat
类进行 Java 字符串插值
MessageFormat
类提供了一个 format()
方法,可以用来执行字符串插值。在这里,我们用占位符 ({0}, {1} ...)
替换字符串中的变量。请看下面的例子。
import java.text.MessageFormat;
public class SimpleTesting{
public static void main(String[] args) {
String site_name = "Jiyik";
String type = "'How to Guide'";
String message = MessageFormat.format("{0} is a {1} Portal", site_name, type);
System.out.println(message);
}
}
输出:
Jiyik is a 'How to Guide' Portal
使用 Java 中的 StringBuilder
类进行 Java 字符串插值
我们可以使用 StringBuilder
类的 append()
方法将变量追加到字符串中。StringBuilder
是 String
的可变版本,可以用变量进行修改。在这里,我们通过插值变量来创建一个字符串。
public class SimpleTesting{
public static void main(String[] args) {
String site_name = "Jiyik";
String type = "'How to Guide'";
StringBuilder message = new StringBuilder(site_name).append(" is a ").append(String.valueOf(type)).append(" Portal");
System.out.println(message);
}
}
输出:
Jiyik is a 'How to Guide' Portal
使用 Java 15 中的 formatted()
方法进行 Java 字符串插值
如果你正在使用 Java 15 或更高版本,你可以使用 formatted()
方法。Java 在 Java 15 版本中加入了这个方法,以实现字符串格式化。这个方法需要一个 Object[]
类型的单一参数。请看下面的例子。
public class SimpleTesting{
public static void main(String[] args) {
String site_name[] = {"Jiyik","How to Guide"};
String message = "%s is a '%s' Portal".formatted(site_name);
System.out.println(message);
}
}
输出:
Jiyik is a 'How to Guide' Portal
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。