A lot of times we're sensitive. Google follows a large number of MVC architectures when designing Android, allowing it to be independent of writing public code, artwork, and concrete logic developers. How do you implement a formatted string in the Android resource file Values/strings.xml? Here's a simple example of Android123, and where you might end up using it.
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "App_name" >cwj_Demo</string>
<string name= "Hello" >android Development Network </string>
</resources>
The above is a simple string resource file that is not formatted because it is simpler to describe the meaning when we design a Delete xxx file. , we may need to dynamically get the name of XXX in Java, so it's easy to use formatting when defining resources, and don't need a bunch of strings to splice or stringbuffer a append such a stupid way to see an example
<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, a formatted string function like printf, Of course, if you include multiple content that needs to be formatted, the second one can be written as%2$s or%2$d, so how do you ultimately invoke it in Java? Look at the following example:
Example one: an integral type of
<string name= "alert" >i am%1$d years old</string> defines this.
Of course, we put an end to unexpected situations, such as a string of secret, note that%1$d is not%1 $ s, so the default standard merge becomes
int nage=23;
String Sageformat = Getresources (). getString (R.string.alert);
String sfinalage = String.Format (Sageformat, nage);
After this execution, it is composed of I am years old, is not very convenient AH. Of course, here's what happens when you look at string strings.
Example two: String type
String sname= "CWJ"
String scity= "Shanghai"
Resource is defined as <string name= "Alert2" >my name is%1 $ s, I am form%2$s</string>
In Java, you need only
String Sinfoformat = Getresources (). getString (R.STRING.ALERT2);
String Sfinalinfo=string.format (Sinfoformat, sname, scity);
We see the whole, the entire definition is similar to MFC's Cstring::format or Mac OS NSLog, but need to display similar to C # in the display of the parameters of the number, such as%1 or%n, where the number represents the nth parameter. The final sfinalinfo display of the Bank is:
My name is CWJ, I am form Shanghai.