使用 % 在 Java 中进行字符串格式化
以下代码使用 % 在 Java 中实现了字符串格式化 −
示例
public class Demo {
public static void main(String args[]){
String my_str = " sample.";
String concat_Str = String.format("This is a" + "%s", my_str);
String format_str_1 = String.format("The value is %.4f", 78.92367);
System.out.println(concat_Str);
System.out.println(format_str_1);
}
}输出
This is a sample. The value is 78.9237
名为 Demo 的类包含主函数。在此处定义了一个字符串值,用于通过将其连接到另一个变量来格式化该字符串。同样,还使用“%”运算符格式化了浮点数。这两个值都打印在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP