String-format Style Use

Source: Internet
Author: User

First, let's look at the following code

protectedString calcu1 () {StringBuffer RESULTB=NewStringBuffer (); String Str=NULL; STR= String.Format ("hi,%s", "Wang Li"); Resultb.append (str+ "|"); STR= String.Format ("hi,%s:%s.%s", "Wang Nan", "Li Li", "Wang Zhang"); Resultb.append (str+ "|"); STR= String.Format (locale.us, "%" + 3 + "." + 4 + "G",                136985021.96584239551); Resultb.append (str+ "|"); SimpleDateFormat format=NULL; if(Iszh ( This) ) {format=NewSimpleDateFormat ("yyyy mm month DD Day"); } Else{format=NewSimpleDateFormat ("Yyy-mm-dd"); } STR= Format.format (NewDate (1000000000*2000l)); Resultb.append (str+ "|"); STR=getresources (). getString (R.string.test); STR= String.Format (str, "yy")); Resultb.append (str+ "|"); STR=getresources (). getString (R.string.test1); STR= String.Format (str, "Henan", "Xiayi", 33); Resultb.append (str+ "|"); STR=resultb.tostring (); returnstr; }

The resource files are as follows

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "Test">My name is: \ "%1$s\"</string>    <stringname= "Test1">I come from: \ "%1$s\" province, \ "%2$s\" county, this year \ "%3$d\" years old</string></Resources>

The results of the operation are as follows

The above is actually mainly related to the following two points of knowledge

1) How to use formatted strings in XML

This method is also relatively simple, mainly when defining strings in XML, add the placeholder you want to change, the common placeholder class is the same as the rules in C language,

S: string

C: Character

D: Integer

F: Floating point number

In the form of%1 $ s

% placeholder

1$ the first one

s string

When used, it corresponds to the method String.Format (String,s1,s2,s3 .... The second third of ... Parameters

String-format Style Use

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.