Java format string
Description
The Android string resource definitions allow standard Java string-formatting sequences.
Example
The following code defines Java format string which we can use in the Java code.
This XML string resource file needs to be in the /res/values
subdirectory.
<resources>
<string name="simple_string">simple string</string>
<string name="java_format_string">
Hi %2$s Java format string. %1$s again
</string>
</resources>
The following code shows how to use Java format string in Java code.
String simpleString = activity.getString(R.string.simple_string);
textView.setText(simpleString);//from w w w. jav a 2s. com
String javaFormatString = activity.getString(R.string.java_format_string);
String substitutedString = String.format(javaFormatString, "Hello" , "Android");
textView.setText(substitutedString);
Note
java_format_string
is defined in the resource xml file.
We use the Activity.getString
to load the string to Java code.