Formatting an array of strings with arguments

I would like to format an array of strings just like the android used to format strings :

Usually we do:

  • strings.xml

    <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string> 
  • In some java code:

     Resources res = getResources(); String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); 

I am looking for something like:

  • in some arbitrary xml:

      <string-array name="employee"> <item>name: %1$s</item> <item>post: %2$s</item> </string-array> 
  • in some java code:

     Resources res = getResources(); String[] employee = ArrayString.format(res.getStringArray(R.string.employee), name, post); 

Is there an elegant way to do this?

EDIT:

The following code snippets are a workaround, and I am posting it just to help @Sufian, who asked for his comment. This is not a real answer, as soon as my question is to format the contents of an array of strings, and the following code formats each row separately.

In some misc.xml:

 <string-array name="string_array"> <item>1st position: %1$d</item> <item>2nd position: %1$d</item> </string-array> 

Then in java code:

 res = getResources(); String[] sa = res.getStringArray(R.array.string_array); for (int i = 0; i < sa.length; i++ ) { text += String.format(sa[i], i); } 
+7
source share
2 answers

Just use:

 String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2); 
0
source

All Articles