objective c - How do I left pad an NSString to fit it in a fixed width? -
i have nsstring strings represent times in hh:mm:ss hh , digit of mm may omitted.
i need align columns of these values this:
1:01:43 43:23 7:00
what's easiest way this?
make use of stringwithformat method provided in nsstring class. this:
nsstring * datestr = @"05:30"; nsstring * formattedstr = [nsstring stringwithformat:@"%8s", [str utf8string]];
the 8 in %8s number of precision digits (or in case characters) want in formatted string. input strings shorter specified precision left padded spaces, strings longer truncated. can read more format strings printf here.
edit @peter pointing out. there (minor) differences between format specifiers in standard c, vs objective c. refer apples documentation on format string specifiers here.
Comments
Post a Comment