Jagan
Jagan

Reputation: 642

String format parameter is not working in string.xml on android

in my code i'm sending string parameter to the string.xml using this

String.format(getString(R.string.notification_devil_expire_coupon_for_one_user), "iphone", String.valueOf(loggedInUser.getExpiryReminder()));

in string.xml im using this to get the parameter

 <string name="notification_devil_expire_coupon_for_one_user">Do you even shop, bro? Your <xliff:g id="retailer">%s1</xliff:g> coupon expires in <xliff:g id="endDate">%s2</xliff:g> days.</string>

expected output

 Do you even shop, bro? Your iphone coupon expires in 3 days.

but my output is

 Do you even shop, bro? Your iphone1 coupon expires in 32 days.

parameter number is added in the string. i dont know where i'm doing wrong in the code

Upvotes: 1

Views: 1117

Answers (2)

Mark Gj&#248;l
Mark Gj&#248;l

Reputation: 1882

Numbering your arguments is good practice since the order may change depending on language. Where just one argument is %s, several will be %1$s, %2$s etc.

Upvotes: 1

Ben Glasser
Ben Glasser

Reputation: 3355

the string formatter substitutes %s with values in an ordered list. There is no need to number them. So rather than using %s1 and %s2 just use %s in each place like this

<string name="notification_devil_expire_coupon_for_one_user">Do you even shop, bro? Your <xliff:g id="retailer">%s</xliff:g> coupon expires in <xliff:g id="endDate">%s</xliff:g> days.</string>

Upvotes: 3

Related Questions