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

3

13

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.

Parlando answered 4/11, 2009 at 3:27 Comment(1)
hahaha +1 for ghetto-debugging tagFirebreak
H
17

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.

Hockett answered 4/11, 2009 at 5:3 Comment(2)
what is you have a complete string (e.g 1 Item1 6 0% 6 ) how would that be?Surprint
@Anthony Cramp : it won't work if string length is more....What if want to trim string if string length is more than column length...Flyer
J
1

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]);
Judkins answered 4/11, 2009 at 3:37 Comment(3)
What do you get if the word is "tree". I get "word:tree rank:4" which doesn't seem to react to the column specifier.Parlando
@willc2: I've edited my answer with Anthony's information, give that a try.Judkins
@JedSmith, may I know how to do it in Swift? TYIAFarly
B
0

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.

Ballew answered 21/9, 2011 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.