question
In development some part of the middle of a string needs to be mutable. For example, the text should show "User Zhang San is from Shenzhen, age 18, gender male..."
Among them, Zhang San is the user name, and each user is different;
Region Shenzhen is variable String data; age 18 is variable int data; gender Male is variable String data.
Solution
Use String.format method to solve.
public static void main(String[] args) { String name = "Zhang San"; String city = "Shenzhen"; int age = 18; String sex = "male"; String format = String.format("%s user from%s,age %d,gender %s.....", name, city, age, sex); System.out.println(format); }
String.format() Description
1. Two overloaded methods of String.format() string
- format(String format, Object… args): The new string uses the locale, specifying the string format and arguments to generate a formatted new string.
- format(Locale locale, String format, Object… args): Use the specified locale, specify the string format and parameters to generate a formatted string.
2. Placeholder Type
converter | Detailed description | Example |
---|---|---|
%s | String type | “Worthy of praise” |
%c | character type | 'h' |
%b | boolean type | true |
%d | Integer type (decimal) | 88 |
%x | Integer type (hex) | FF |
%o | Integer type (octal) | 77 |
%f | floating point type | 7.777 |
%a | hexadecimal floating point type | FF.35AE |
%e | Index type | 9.38e+5 |
%g | Generic floating point type (the shorter of the f and e types) | |
%h | hash code | |
%d% | percentage type | |
%n | newline | |
%tx | date and time type |
Example description
public static void main(String[] args) { String str; // %s str = String.format("Hi,%s", "Bruce"); System.out.println(str); // %c %n str = String.format("letter c The capitalization is:%c %n", 'C'); System.out.println(str); // %b str = String.format("The boolean result is:%b", 3>2); System.out.println(str); // %d str = String.format("100 half is:%d", 100/2); System.out.println(str); // %x str = String.format("100 The hexadecimal number is:%x", 100); System.out.println(str); // %o str = String.format("100 The octal numbers are:%o", 100); System.out.println(str); // %f str = String.format("50 yuan book hit 8.5 The discount is:%f Yuan", 50 * 0.85); System.out.println(str); // %a str = String.format("The hexadecimal number of the above price is:%a", 50 * 0.85); System.out.println(str); // %e str = String.format("The index of the prices above means:%e", 50 * 0.85); System.out.println(str); // %g str = String.format("The exponent and floating point results of the above prices have shorter lengths:%g", 50 * 0.85); System.out.println(str); // %d% str = String.format("The discounts above are:%d%%", 85); System.out.println(str); // %h str = String.format("letter A The hash code is:%h",'A'); System.out.println(str); }
output result
3. Commonly used logos
logo | illustrate |
---|---|
+ | Add sign to positive or negative numbers |
0 | Add 0 where there are not enough digits |
space | Fill in spaces where there are not enough digits |
, | Group numbers, separated by three digits, can only be used in decimal |
( | Use parentheses to enclose negative numbers with the minus sign removed |
# | Add OX to the hexadecimal number and o to the octal number; The use of auxiliary %x and %o is equivalent to a supplementary explanation prompt for the number system |
< | Format the argument described by the previous converter lock |
- | Left-aligned, fill in spaces where there are not enough digits |
Example description
public static void main(String[] args) { // + String str; str = String.format("The positive and negative representations of numbers:%+d %d %+d %d",8,8,-8,-8); System.out.println(str); // - str = String.format("Align left:%-6d",8); System.out.println(str); // 0 str = String.format("Missing zeros:%06d",8); System.out.println(str); // space str = String.format("Fill in missing spaces:% 6d",8); System.out.println(str); str = String.format("Fill in missing spaces:% 6d",-8); System.out.println(str); // , str = String.format("Grouping of numbers:%,d",123456789); System.out.println(str); // ( str = String.format("Parentheses usage:%(d",-8888); System.out.println(str); str = String.format("Parentheses usage:%(d",8888); System.out.println(str); // # str = String.format("#Bracket usage(hex): %#x",12); System.out.println(str); str = String.format("#Bracket usage(Octal): %#o",12); System.out.println(str); // < str = String.format("<Parentheses usage:%f %<3.1f",3.14,3.2); // The object of "%<3.1f" is the object of the previous "%f" System.out.println(str); }
output result
4. Date Converter
logo | illustrate |
---|---|
c | Include full date and time information |
F | "Year-Month-Day" format |
D | 'Month/Day/Year' format |
r | "HH:MM:SS PM" format (12-hour clock) |
T | "HH:MM:SS" format (24 hours) |
R | "HH:MM" format (24-hour clock) |
Example description
public static void main(String[] args) { String str; // c str = String.format("Full date and time information:%tc",new Date()); System.out.println(str); // F str = String.format("year-moon-Day format:%tF",new Date()); System.out.println(str); //D str = String.format("moon/day/Year format:%tD",new Date()); System.out.println(str); //r str = String.format("HH:MM:SS PM Format (12 o'clock):%tr",new Date()); System.out.println(str); //T str = String.format("HH:MM:SS Format (24-hour time):%tT",new Date()); System.out.println(str); //R str = String.format("HH:MM Format (24-hour time):%tR",new Date()); System.out.println(str); // %.2f with two decimal places str = String.format("3.14 Two decimal places are reserved:%.2f",3.141); System.out.println(str); }
output result
It's not easy to create, don't forget to give a like, you can let more people see this article, and by the way, encourage me to write a better blog