today in the study of the code written by the predecessor, suddenly found string inside the <stringname="Item_recent_photo" > recent shooting%1$s< /string>
A bit of a circle, this is not the C voice inside the expression, Baidu a bit, remember ...
In TextView you want to display some values dynamically, use the%1$s,%1$d, first introduce:
- %n$ms: Represents the output of a string, n is the number of parameters, set the value of M can be placed before the output space
- %N$MD: Represents the output of an integer, n is the number of arguments, setting the value of M can place a space before the output
- %N$MF: Represents the output is 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
- %d (represents an integer)
- %f (indicates floating point number)
- %s (representing string)
use:1, the whole type, such as "Miss 23 years old this year", this 23 is the integer type. This can be written in String.xml,<string name= "old" > Miss%1$d this year </string>
String Sageformat = Getresources (). getString (R.string.old);
String sfinalage = String.Format (Sageformat, 23);
Replace the%1$d with a.%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 is written as:%2$d, and so on2, 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>
View Sourceprint? 1 String sageformatstring sageformat1= getresources (). getString (R.string.alert);
String sFinal1 = String.Format (SAGEFORMAT1, "John Doe","capital Beijing");
Usage of%1 $ s,%1$d, etc. in Android String.xml