String.format() string formatting

question

During development, a certain part in the middle of a string needs to be variable. For example, a Textview needs to display "XXX user is from Shanghai, age 21, gender male", where 
XXX is the user name, and each user is also different. 
Shanghai is variable. string data 
age 21 is variable int data 
gender male is variable string data 
how do you solve this situation? save this string in the constant class? No! we should follow Google's development mode

JAVA

String userName="XXX";
String userProvince="上海";
int userAge=21;
String userSex="男";
String string=getResources().getString(R.string.user_info);
String userInfo=String.format(string,userName,userProvince,userAge,userSex);

Two overloaded methods of String.format() string general type formatting

  • format(String format, Object... args) new string uses the locale, specifies the string format and parameters to generate a formatted new string.
  • format(Locale locale, String format, Object... args) Generates a formatted string using the specified locale, string format and arguments.

The last chestnut is useful for the formatting of character types and integer types. Below I will list the commonly used types.

conversion character Detailed description example
%s string type "Favorite if you like it"
%c character type ‘m’
%b Boolean type true
%d integer type (decimal) 88
%x integer type (hexadecimal) FF
%o integer type (octal) 77
%f floating point type 8.888
%a hexadecimal floating point type FF.35AE
%e index type 9.38e+5
%g Generic floating-point type (the shorter of f and e types) No example (basically not used)
%h hash code No example (basically not used)
%% percentage type %(%special characters%% can be displayed%)
%n line break No example (basically not used)
%tx Date and time type (x represents different date and time conversion symbols) No example (basically not used)

For the sake of understanding, let's give an example

String str=null;
str=String.format("Hi,%s", "Xiaochao");
System.out.println(str);
str=String.format("Hi,%s %s %s", "Xiaochao","It's a","Big handsome guy");
System.out.println(str);
System.out.printf("The uppercase letter c is: %c %n", 'C');
System. out.printf("Boolean result is: %b %n", "Xiaochao".equal("Handsome guy"));
System.out.printf("Half of 100 is: %d %n", 100/2) ;
System.out.printf("The hexadecimal number of 100 is: %x %n", 100);
System.out.printf("The octal number of 100 is: %o %n", 100);
System .out.printf("The discount of 8.5% for the 50 yuan book is: %f yuan%n", 50*0.85);
System.out.printf("The hexadecimal number of the above price is: %a %n", 50 *0.85);
System.out.printf("The index of the price above: %e %n", 50*0.85);
System.out.printf("The length of the index and floating point result of the price above is shorter: %g %n", 50*0.85);
System.out.printf("The discount above is %d%% %n", 85) ;
System.out.printf("The hash code of letter A is: %h %n", 'A');

output result

With the conversion character and the implementation of advanced functions, $ is used in the first example

the sign illustrate example result
+ Add a sign to positive or negative numbers (“%+d”,15) +15
0 Add 0 in front of the number (commonly used in encryption) (“%04d”, 99) 0099
space Add the specified number of spaces before an integer (“%4d”, 99) 99
, Group numbers with "," (commonly used to display the amount) (“%,f”, 9999.99) 9,999.990000
( Use parentheses to enclose negative numbers (“%(f”, -99.99) (99.990000)
# Include a decimal point if it is a floating point number, add 0x or 0 if it is hexadecimal or octal (“%#x”, 99)(“%#o”, 99) 0x63 0143
< Formats the argument described by the previous conversion specifier ("%f and %<3.2f", 99.45) 99.450000 and 99.45
d,%2$s”, 99,”abc”) 99,abc

In the first example, it was mentioned that %tx x represents the date conversion character, and I will also list the date conversion character by the way.

the sign illustrate example
c Include full date and time information Sat Oct 27 14:21:20 CST 2007
F "Year-Month-Day" format 2007-10-27
D "MM/DD/YYYY" format 10/27/07
r "HH:MM:SS PM" format (12-hour format) 02:25:51 PM
T "HH:MM:SS" format (24 hour format) 14:28:16
R "HH:MM" format (24 hour format) 14:28

Let's use an example to understand

Date date=new Date();
//c的使用
System.out.printf("全部日期和时间信息:%tc%n",date);
//f的使用
System.out.printf("年-月-日格式:%tF%n",date);
//d的使用
System.out.printf("月/日/年格式:%tD%n",date);
//r的使用
System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);
//t的使用
System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);
//R的使用
System.out.printf("HH:MM格式(24时制):%tR",date);

 output result

 

Guess you like

Origin blog.csdn.net/weixin_42218169/article/details/130269874