在 Android 中,字符串中的 %
后面跟着的字符称为格式化占位符,用于指示在运行时将值插入到字符串中的位置,并指定插入的值的类型。常见的格式化占位符类型如下:
- String name = "John";
- String greeting = String.format("Hello, %s!", name);
- int age = 30;
- String message = String.format("My age is %d", age);
- double price = 19.99;
- String label = String.format("The price is $%.2f", price);
- char grade = 'A';
- String feedback = String.format("My grade is %c", grade);
- boolean isOnline = true;
- String status = String.format("Is online? %b", isOnline);
- int value = 255;
- String hexValue = String.format("Hex value is %X", value); // 输出 "Hex value is FF"
%%
。String percent = "10%";
这些是一些常见的格式化占位符类型。你可以根据需要组合它们,以满足不同类型的字符串格式化要求。注意,格式化字符串的方法可以是 String.format()
,printf()
,TextUtils
类的 String.format()
方法,或者其他支持字符串格式化的工具。