8

I'd like to format an array of strings just like 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'm 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 that?

EDIT:

The next pieces of code is a workaround and I'm posting it just to help @Sufian, who asked for it in a comment. It's not a real answer once my question is about format the string array's content and the bellow code is formatting each string 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);
}
4
  • So, how did you solve it? I have a similar issue. Commented Nov 1, 2013 at 9:59
  • It seems that there is no elegant way to perform such formatting. You will need to iterate in each string array element and format it as your needs. Commented Nov 2, 2013 at 18:40
  • Hmm. Any sample could which you could share? For now I'm just picking Strings by name using the following code snippet, and providing formatting arguments like with simple resource Strings. steven.bitsetters.com/2007/11/27/… Commented Nov 3, 2013 at 7:27
  • I edited the answer with a workaround. I hope it helps you. Commented Nov 5, 2013 at 17:40

2 Answers 2

2

Just use:

String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2);
Sign up to request clarification or add additional context in comments.

Comments

0

getQuantityString may solve your problem.

Look at quantity strings in http://developer.android.com/guide/topics/resources/string-resource.html

Here's the specific API doc: http://developer.android.com/reference/android/content/res/Resources.html#getQuantityString(int,%20int,%20java.lang.Object...)

1 Comment

@Chuck_Norris quantity strings actually selects one kind of formatting in a set of many possibilities based in the amount such as zero, few, many... I need to use the entire array string and each array position may be a string formatted with arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.