objective-c code to right pad a NSString?

若如初见. 提交于 2019-11-27 22:14:15

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!