English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
%1$s %1$d Android string (java & Android formatting string)
1$s // String
%1$d // int
//R.string.old:
<string name="old">I am%1$d years old</string>
String sAgeFormat = getResources().getString(R.string.old);
String sFinalAge = String.format(sAgeFormat, 23);
1, integer type, such as “I am23years old”, this23is an integer type. In string.xml, it can be written as, <string name="old">I am%1$d years old</string>
In the program, use
String sAgeFormat = getResources().getString(R.string.old);
String sFinalAge = String.format(sAgeFormat, 23);
Replace%1$d is replaced with23;
%1$d means the replacement of the first integer in the entire name="old". If there are two integer contents to be replaced in a name, the second one is written as:%2$d, etc.; see the following string type for specific program replacement;
2, string type, such as "My name is Li Si, I come from the capital Beijing"; both "Li Si" and "Capital Beijing" need to be replaced.
In string.xml, it can be written as, <string name="alert">My name is%1$s, I come from%2$s</string>
In the program:
String sAgeFormatString sAgeFormat1= getResources().getString(R.string.alert);
String sFinal1 = String.format(sAgeFormat1, "Li Si", "Capital Beijing");
Here, the two strings to be replaced correspond in order according to the above program.
Introduction to <xliff:g> tag:
The attribute id can be named arbitrarily
Example of attribute value explanation
%n$ms: Represents a string, n represents the nth parameter, setting the value of m can place a space before the output
%n$md: Represents an integer, n represents the nth parameter, setting the value of m can place a space before the output, or can be set to 0m, placing m zeros before the output
%n$mf: Represents a floating-point number, n represents the nth parameter, setting the value of m can control the number of decimal places, such as m=2.2The output format is 00.00 when
It can also be written simply as:
%d (Represents an integer)
%f (Represents a floating-point number)
%s (Represents a string)
Example of usage 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">Xiao Hong this year<xliff:g id="xxx">%d</xliff:g></string>/xliff:g>years old, went to school at<xliff:g id="yyy">%s</xliff:g>/xliff:g>grade!</xliff:g>/string>
3.
String test = String.format(getResources().getString(R.string.test_xliff), 7, "Second Grade小学");
Output:
Xiao Hong this year7is in second grade of elementary school!
This is the collection of materials on Java and Android string output. More related materials will be supplemented in the future, thank you all for your support to this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)