Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format resource string with leading zero? [duplicate]

Tags:

string

android

Im using strings with placeholder in my strings.xml

<string name="date">%1$d.%2$d.%3$d</string>

And set it in the code like:

String.format(context.getResources().getString(R.string.date), day, month, year);

If we take the 5th may for example the result now is:

5.5.2015

How can i add the leading zero for numbers smaler than ten to the string resources?

Not a duplicate of Left padding a String with Zeros since the solution of this question is:

String.format("%010d", Integer.parseInt(mystring));

But the format for the android resource string is:

%1$d

where the 1 indicates the index. Where should i put the 02?

like image 871
Mulgard Avatar asked Sep 19 '15 13:09

Mulgard


1 Answers

You can use this format:

<string name="date">%1$02d.%2$02d.%3$d</string>

For further studying about format, see Formatter's sections: conversion, flags and width.

like image 95
hata Avatar answered Sep 28 '22 04:09

hata