Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java String.format ()

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use Java String.format ()". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

Format (String format, Object... Args) the new string uses the local locale to set the string format and parameters to generate a new formatted string.

Format (Locale locale, String format, Object... Args) uses the specified locale to develop string formats and parameters to generate formatted strings.

Common types:

For example:

1. String str=null; 2. Str=String.format ("Hi,%s", "Xiao Chao"); 3. System.out.println (str); 4. Str=String.format ("Hi,%s% s% s", "Xiao Chao", "Shi", "handsome guy"); 5. System.out.println (str); 6. System.out.printf (the capital of the letter c is:% c% n,'C') 7. System.out.printf ("Boolean result is:% b% n", "small super" .handsome); 8. System.out.printf ("half of 100 is:% d% n", 100max 2); 9. System.out.printf ("hexadecimal number of 100 is:% x% n", 100); 10. System.out.printf ("octal number of 100 is:% o% n", 100) 11. System.out.printf ("book discount of 50 yuan is:% f% n", 50% 0.85); 12. System.out.printf ("hexadecimal number of the above price is:% a% n", 50% 0.85); 13. System.out.printf ("the index of the above price indicates:% e% n", 50% 0.85) 14. System.out.printf ("the shorter length of the index and floating point results of the above price is:% g% n", 50% 0.85); 15. System.out.printf ("the above discount is% d% n", 85); 16. System.out.printf ("the hash code of the letter An is:% h% n",'A') Output 1. Hi, Xiao Chao 2. Hi, Xiao Chao is a handsome guy 3. The capitalization of the letter c is: C 4. The result of Boolean is that half of false 5. 100 is 50 6. 100 hexadecimal number is 64 7. 100 octal number is 1 444 8. 50 yuan discount is 42.500000 yuan 9. The hexadecimal number of the above price is: 0x1.54p5 10. The index of the above price says: 4.250000e+01 11. The shorter length of the index and floating point result of the above price is: 42.5000 12. The above discount is 85% 13. The hash code for the letter An is: 41 # with conversion characters and advanced functions $is used in the first example

In the first example, it is said that% tx x stands for the date conversion character, and I also list the date conversion character by the way.

For example:

1. Date date=new Date (); 2. / / c use 3. System.out.printf ("all date and time Information:% tc%n", date); 4. / / f use 5. System.out.printf ("year-month-day format:% tF%n", date) 7. System.out.printf ("month / day / year format:% tD%n", date); 8. / / r use 9. System.out.printf ("HH:MM:SS PM format (12:00):% tr%n", date); 10. / / t use 11. System.out.printf ("HH:MM:SS format (24:00):% tT%n", date) 12. / / R uses 13. System.out.printf ("HH:MM format (24:00):% tR", date); output result 1. All date and time information: Wednesday September 21 22:43:36 CST 2016 2. Year-month-day format: 2016-09-213. Month / day / year format: 16-10-21 4. HH:MM:SS PM format (12:00 system): 10:43:36 afternoon 5. HH:MM:SS format (24:00 system): 22:43:36 HH:MM format (24:00 system): 22:43 "how to use Java String.format ()" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report