Usage of%1 $ s,%1$d, etc. in Android String.xml

Source: Internet
Author: User

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

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.