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!