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的类包含主函数。这里定义了一个字符串值,通过将其连接到另一个变量来格式化该字符串。类似地,也使用%运算符对浮点数进行格式化。这两个值都打印在控制台上。
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短