开发者

Is it possible to use format strings to align NSStrings like numbers can be?

I'm using NSLog() to print some tabular data consisting of an NSString and an associated integer.

Assume I know the length of the longest word.

Is there a way using format strings to get this kind of column alignment:

word:tree        rank:5  
word:frog        rank:3  
word:house       rank:2  
word:peppercorn  rank:2  
word:sword       rank:2  
word:antlion     rank:1  

The reason I'm asking about formatting strings is I'm开发者_运维问答 hoping for a lightweight way to format my ghetto debugging output.

Here is what I tried:

NSString *word = @"tree";
NSUInteger rank = 4;
NSString *str = [NSString stringWithFormat:@"word:%-20@ rank:%u", word, rank];
NSLog(@"%@", str);

Result:

word:tree rank:4

No effect at all.


The following seems to work, but requires conversion from your NSString's to C-strings.

NSString *word = @"tree";
NSUInteger rank = 4;
NSString *str = [NSString stringWithFormat:@"word:%-20s rank:%u", [word UTF8String], rank];
NSLog(@"%@", str);

Don't know why the field width is being ignored when trying to use an NSString.


Yes, just like printf.

According to the documentation, stringWithFormat: obeys the IEEE printf specification, which allows all kinds of modifications on the individual arguments. The documentation has a restricted subset of that information, but they link to the OpenGroup printf specification for Unix to give the full information (worth a read, you can accomplish a lot of tricks with format specifiers).

Try this, to get exactly what you've pasted above:

NSString *word = @"butterfly";
NSUInteger rank = 4;
NSString *str = [NSString stringWithFormat:@"word:%-11s rank:%u", [word UTF8String], rank];

Here's an example of how I format my debugging output (I don't use NSLog, I wrap printing to standard error to get file and line, too):

fprintf(stderr, "%s | %30s:%-5d | %s", [[[NSDate date] description] UTF8String],
    [fileName UTF8String], line, [body UTF8String]);


If you do something like the earlier answer:

NSString *word = @"tree";
NSUInteger rank = 4;
NSString *str = [NSString stringWithFormat:@"word:%-20s rank:%u", [word UTF8String], rank];
NSLog(@"%@", str);

... you can get encoding-conversion problems for non-ASCII characters... stringWithFormat seems to assume the system default encoding, which is still MacRoman for some crazy reason. You can drop down to the stdlib level -- do all your formatting with sprintf into your own buffer, and then you can control the encoding when creating an NSString from that -- but that's cumbersome. If anyone knows a convenient workaround, I'm all ears.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜