For example, I need the NSString have at least 8 chars....instead of using a loop to add the left pad spaces on this, is there anyway to do it?
Examples:
Input: |Output:
Hello | Hello
Bye | Bye
Very Long |Very Long
abc | abc
Here is an example of how you can do it:
int main (int argc, const char * argv[]) {
NSString *str = @"Hello";
int add = 8-[str length];
if (add > 0) {
NSString *pad = [[NSString string] stringByPaddingToLength:add withString:@" " startingAtIndex:0];
str = [pad stringByAppendingString:str];
}
NSLog(@"'%@'", str);
return 0;
}
I just do something like this:
NSLog(@"%*c%@", 14 - theString.length, ' ', theString);
Moreover, 14
is the width that you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With