%1 $ s%1$d Android string

Source: Internet
Author: User

1, integral type, for example "I am 23 years old this year", this 23 is the integer type. In the String.xml can write this, <string name= "old" > I%1$d years of age </string>


In the program, use the

String Sageformat = Getresources (). getString (R.string.old); String sfinalage = String.Format (Sageformat, 23);

Replace the%1$d with 23;


%1$d expression means the substitution of the first integer in the entire name= "old". If there are two integer contents in a name that need to be replaced, the second one is written as:%2$d, and so on, and the following string is substituted for the specific program;


2, String type, such as "My name is John Doe, I come from the capital Beijing"; Here "John Doe" and "capital Beijing" need to be replaced.


This can be written in String.xml, <string name= "alert" > My name is%1 $ s, I'm from%2$s</string>


In the program:

View sourceprint?1 String sageformatstring sageformat1= getresources (). getString (R.string.alert); String SFinal1 = String.Format (SAGEFORMAT1, "John Doe", "capital Beijing");

Here are two strings that need to be replaced, corresponding in the order of the programs above.

<xliff:g > tag Description:  


%n$ms: Represents the output of a string, n is the number of parameters, setting the value of M can be placed before the output space  
%N$MD: Represents the output is an integer, n is the number of parameters, set the value of M can be placed before the output space, or set to 0m, before the output of M 0 
%N$MF: Represents the output of a floating-point number, n is the number of parameters, setting the value of M can control the scale, such as m=2.2, the output format is 00.00 

can also be simply written as:

%d (represents an integer)

%f (indicates floating point number)

%s (representing string)

Examples of use steps:

1.

<?xml version= "1.0" encoding= "Utf-8"?>

<resources xmlns:xliff= "urn:oasis:names:tc:xliff:document:1.2" >

2.

<string name= "Test_xliff" > Little Red this year <xliff:g id= "xxx" >%d</xliff:g> years old, on <xliff:g id= "yyy" >%s</ Xliff:g> Grade! </string>

3.

String test = String.Format (Getresources (). getString (R.string.test_xliff), 7, "Primary School II");

Output:

Little Red this year 7 years old, on the second grade of primary school!


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.