Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help in padding numerical strings

I am trying to generate a numerical string by padding the number with zeroes to the left.

0 would become 00000 1 would become 00001 10 would become 00010

I want to create five character NSString by padding the number with zeroes.

I read this Create NSString by repeating another string a given number of times but the output is an NSMutableString.

How can I implement this algorithm with the output as an NSString?

Best regards.

like image 957
Ali Avatar asked Mar 18 '11 23:03

Ali


1 Answers

You can accomplish this by calling

[NSString stringWithFormat:@"%05d", [theNumber intValue]];

where theNumber is the NSString containing the number you want to format.

For further reading, you may want to look at Apple's string formatting guide or the Wikipedia entry for printf.

like image 169
jonmorgan Avatar answered Nov 02 '22 07:11

jonmorgan