迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中格式化字符串

作者:迹忆客 最近更新:2023/03/26 浏览次数:

本篇文章介绍了如何在 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() 方法将变量追加到字符串中。StringBuilderString 的可变版本,可以用变量进行修改。在这里,我们通过插值变量来创建一个字符串。

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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便