18

Let's say I have this string:

Your player has a good keeper skill and a decent people skill

Now the part not in bold of the string is always the same, what is known at runtime is the part in bold.

So how could I do something like:

Your player has a {var1} keeper skill and a {var2} people skill

and then fill those vars at runtime with right values?

I do not want to concatenate strings like:

"You player has a"  + var1  + "keeper skill and a"  + var2 + "people skill"
1
  • Resources.getString(resId, ...) or Context.getString(resId, ...) with string like "You player has a %1$s keeper skill and a %2$s people skill" is what you are looking for? Commented Oct 4, 2011 at 10:46

4 Answers 4

46

You need to see Android string resource guide. There is a way to provide static string which can be later formatted with variables.

http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

You will define string like

<resources>
  <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
</resources>

And later in code you can replace

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
Sign up to request clarification or add additional context in comments.

3 Comments

tnx, your getting the answer accepted for completeness in links and code.
more info about formatting string tokens at developer.android.com/reference/java/util/Formatter.html
'fromHtml(String!): Spanned!' is deprecated. Deprecated in Java
11

in Strings.xml

You player has a %1$d  keeper skill and a %2$d people skill

in java

getString(R.string.article_stats, var1, var2);

Comments

8

Yes, see the following from android devguide

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application like this:

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

Comments

3

in strings xml you have define html syntax in CDATA tag like

<![CDATA[<b> %1$s bought </b>, last purchased from %2$s <b> %3$s </b>]]>

and in your java class

String detail =  String.format(getString(R.string.detail),15,"New Delhi","23 mins ago");
        detailView.setText(Html.fromHtml(detail ));

Comments

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.