String. format () format string, String format string

Source: Internet
Author: User

String. format () format string, String format string

1. Several Common conversion Operators

Description of the conversion operator % d Integer type (decimal) 99% f floating point type 99.99% s string type "mingrisoft" % c character type 'M' % B Boolean Type true % Percent type % n linefeed

 

Package com. app; public class Test1 {public static void main (String [] args) {String str1 = String. format ("Hi, % s", "Wang Li"); System. out. println (str1); String str2 = String. format ("Hi, % s: % s. % s "," Wang Nan "," Wang Li "," Wang Zhang "); System. out. println (str2); System. out. printf ("half of 100 is: % d % n", 100/2); System. out. printf ("half of 100 is: % n % d", 100/2); // a line break will be generated when 50 is output }}

Result:
Hi, Wang Li
Hi, Wang Nan: Wang Li. Wang Zhang
Half of 100 is: 50
Half of 100 is:
50

2. Matching identifier

Indicates that the instance result + is a positive number or a negative number is added with the symbol ("% + d", 15) + 15 0 before the number 0 ("% 04d", 99) 0099 $ formatted parameter indexes ("% 1 $ d, % 2 $ s", 99, "abc") 99, abc

 

Package com. app; public class Test1 {public static void main (String [] args) {// $ use String str1 = String. format ("use of format parameter $: % 1 $ d, % 1 $ s", 99, "abc"); System. out. println (str1); String str2 = String. format ("use of format parameter $: % 1 $ d, % 2 $ s", 99, "abc"); System. out. println (str2) ;}} format parameter $ use: 99, 99 format parameter $ use: 99, abc

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.