You can use C language printf formatting with -[NSMutableString appendFormat:]
and all other NSString
"format" methods. It doesn't respect NSString (do formatting on %@
), so you need to convert them to ASCII.
String Padding in C
- (NSString *)sample {
NSArray<NSString *> *input = @[@"Hello", @"Bye", @"Very Long", @"abc"];
NSMutableString *output = [[NSMutableString alloc] init];
for (NSString *string in input) {
[output appendFormat:@"%8s\n", string.UTF8String];
}
return output;
}
/*
Return value:
Hello
Bye
Very Long
abc
*/