Format method for string resource files in Android

Source: Internet
Author: User

Most of the time our sensibility to Google in the design of Android to adhere to a large number of MVC architecture, you can write public code, art and specific logic developers Independent. How do I implement a formatted string for the Android resource file Values/strings.xml?
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >stringdemo</ string>    <string name= "Hello" >hello robert</string></resources>
The above is a simple string resource file, it is not used to format, because it is relatively simple to directly describe the meaning, when we design a similar Delete xxx file? , we may need to dynamically get the name of XXX in Java, so it is easy to use formatting when defining resources, without the need for a bunch of strings to stitch or stringbuffer a silly way to append, see examples
<string name= "alert" >delete%1 $ s file</string>
Here%1 $ s represents this is a string type, if the integer type can be written as%1$d, like printf, such as formatted string function, of course, if it contains more than one need to format the content, the second can be written as%2$s or%2$d, then finally in Java how to call it? Look at the following example:

Example one: the integer type
<string name= "alert" >i am%1$d years old</string>
That's what defines it.
Of course, we eliminate the unexpected, such as the secret of a string type, note that%1$d is not%1 $ s, so the default standard merge becomes
int nage=23; String Sageformat = Getressources (). getString (R.string.alert); String sfinalage = String.Format (Sageformat, NAge);

When this is done, I am years old is formed.

You can also get direct access to

String sfinalage=getresources (). getString (R.string.alert,nage);
Here's what happens when you look at string strings.

Example Two: the String type
String sname= "Robert"; <pre name= "code" class= "java" >string scity= "Yantai";


Resource is defined as
<string name= "Alert2" >my name is%1 $ s, I am form%2$s</string>
In Java, you only need to
String Sinfoformat = Getresources (). getString (R.STRING.ALERT2); String Sfinalinfo=string.format (Sinfoformat, SName, scity);

The bank's final sfinalinfo shows that the content is

My name is Robert, I am form Yantai


Welcome to scan QR Code, follow the public number



Format method for string resource files in Android

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.