简体   繁体   中英

objective-c code to right pad a NSString?

Can someone give a code example of how to right pad an NSString in objective-c please?

For example want these strings:

Testing 123 Long String
Hello World
Short 

if right padded to a column width of say 12: and then a sting "XXX" is added to the end of each, it would give:

Testing 123 xxx
Hello World xxx
Short       xxx

That is a 2nd column would like up.

Adam is on the right track, but not quite there. You do want to use +stringWithFormat:, but not quite as he suggested. If you want to pad "someString" to (say) a minimum of 12 characters, you'd use a width specifier as part of the format. Since you want the result to be left-justified, you need to precede the width specifier with a minus:

NSString *padded = [NSString stringWithFormat:@"%-12@", someString];

Or, if you wanted the result to be exactly 12 characters, you can use both minimum and maximum width specifiers:

NSString *padded = [NSString stringWithFormat:@"%-12.12@", someString];

2nd column of what would line up?

Given that you are on iOS, using HTML or a table view would be far more straightforward than trying to line up characters with spaces. Beyond being programmatically more elegant, it will look better, be more resilient to input data changes over time, and render a user experience more in line with expectations for the platform.

If you really want to use spaces, then you are going to have to limit your UI to a monospace font (ugly for readability purposes outside of specific contexts, like source code) and international characters are going to be a bit of a pain.

From there, it would be a matter of getting the string length (keeping in mind that "length" does not necessarily mean "# of characters" in some languages), doing a bit of math, using substringWithRange: and appending spaces to the result.

Unfortunately, Objective-C does not allow format specifiers for %@. A work-around for padding is the following:

NSString *padded = [NSString stringWithFormat:@"%@%*s", someString, 12-someString.length, ""];

which will pad the string to the right with spaces up to a field length of 12 characters.

%-@ does not work, but %-s works

NSString *x = [NSString stringWithFormat:@"%-3@", @"a" ];
NSString *y = [NSString stringWithFormat:@"%-3@", @"abcd" ];
NSString *z = [NSString stringWithFormat:@"%-3@ %@", @"a", @"bc" ];
NSString *zz = [NSString stringWithFormat:@"%-3s %@", "a", @"bc" ];
NSLog(@"[%@][%@][%@][%@].......", x,y,z,zz);

output: [a][abcd][a bc][a bc].......

Try below. its working for me.

NSString *someString = @"1234";
NSString *padded = [someString stringByPaddingToLength: 16 withString: @"x" startingAtIndex:0];

NSLog(@"%@", someString);
NSLog(@"%@", padded);

First up, you're doing this a bad way. Please use separate labels for your two columns and then you will also be able to use proportional fonts. The way you're going about it you should be looking for an iPhone curses library.

If you really have to do it this way just use stringWithFormat:, like:

NSString *secondColumnString = @"xxx";
NSString *spacedOutString = [NSString stringWithFormat:@"testingColOne  %@", secondColumnString];
NSString *spacedOutString = [NSString stringWithFormat:@"testingAgain   %@", secondColumnString];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM