% 1 $ S % 1 $ D Android string conveniently changes the value of string dynamically

Source: Internet
Author: User
1. Integer. For example, "I am 23 years old this year". This 23 is an integer. This can be written in string. xml. <string name = "old"> I am % 1 $ d this year </string>. [Java]View
Plaincopy

  1. String sageformat = getresources (). getstring (R. String. Old );
  2. String sfinalage = string. Format (sageformat, 23 );
Replace % 1 $ d with 23; % 1 $ D represents the replacement of the first integer in the entire name = "old. If a name contains two integer values to be replaced, the second value is % 2 $ D, and so on. For specific program replacement, see the string type below; 2. string type, for example, "My name is Li Si, and I am from the capital Beijing". Here, "Li Si" and "capital Beijing" must be replaced. You can write this in string. xml. <string name = "alert"> my name is % 1 $ s, and I am from % 2 $ S </string> in the program: [Java]View
Plaincopy

  1. View sourceprint? 1 string sageformatstring sageformat1 = getresources (). getstring (R. String. Alert );
  2. String sfinal1 = string. Format (sageformat1, "Li Si", "capital Beijing ");
Here the two strings need to be replaced, which correspond in sequence according to the above program.

<Xliff: G> label introduction:
The property ID can be named as needed.
Attribute Value example
% N $ MS: indicates that the output is a string, and N indicates the number of parameters. The m value can be set with spaces before the output.
% N $ MD: indicates that the output is an integer, and N indicates the number of parameters. To set M, you can place a space before the output, or set it to 0 m, put M 0 before output
% N $ MF: indicates that the output is a floating point number, and N indicates the number of parameters. Setting the m value can control the number of decimal places. For example, when M = 2.2, the output format is 00.00.

You can also write it as follows:

% D (integer)

% F (floating point number)

% S (string)

 

Example:

1.

<? XML version = "1.0" encoding = "UTF-8"?>

<Resources xmlns: xliff = "urn: Oasis: names: TC: xliff: Document: 1.2">

2.

<String name = "test_xliff"> Xiaohong, <xliff: G id = "XXX"> % d </xliff: G>, aged <xliff: g id = "yyy"> % S </xliff: G> grade! </String>

3.

String test = string. Format (getresources (). getstring (R. String. test_xliff), 7, "2 ");

Output:

Xiaohong is 7 years old. He is in the second grade of primary school!

Statement: This article from http://blog.csdn.net/wufen1103/article/details/7846691

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.