11

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
StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • possible duplicate of [Padding string to left with objective c](http://stackoverflow.com/questions/964322/padding-string-to-left-with-objective-c) – Abizern Dec 28 '11 at 03:17

4 Answers4

18

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;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    Why would you do it this way? This is already what `stringByPaddingToLength:withString:startingAtIndex:` does. You could just get rid of everything after the declaration of `str` and instead do `NSLog(@"%@", [str stringByPaddingToLength:add withString:@" " startingAtIndex:0])` – Chuck Dec 28 '11 at 03:43
  • 1
    @Chuck Yes, that was my initial idea. Unfortunately, my understanding is that the padding method pads only on the right, overwriting the characters from the index on. In other words, if you started at zero, you'd get a string of eight spaces. – Sergey Kalinichenko Dec 28 '11 at 03:48
  • 1
    Ah, you're right, I clearly did not read the question correctly. Never mind me. – Chuck Dec 28 '11 at 03:51
  • @dasblinkenlight I initially thought the same thing about the index. But actually, `startingAtIndex` is for where in the padding string the padding starts. See [NSString Class Reference].(https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByPaddingToLength:withString:startingAtIndex:) – Maple Mar 27 '13 at 20:42
8

I just do something like this:

    NSLog(@"%*c%@", 14 - theString.length, ' ', theString);

Moreover, 14is the width that you want.

PlusA
  • 545
  • 1
  • 6
  • 15
0

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
*/
bshirley
  • 8,217
  • 1
  • 37
  • 43
-2

if you need the same answer in a method, I had to create one for use in my projects. original code by dashblinkenlight

- (NSString *) LeftPadString: (NSString*) originalString LengthAfterPadding: (int)len  paddingCharacter: (char) pad
{
int add = (int) (len - originalString.length);

NSString* paddingCharString = [NSString stringWithFormat:@"%c" , pad];

if (add > 0)
{
    NSString *pad = [[NSString string] stringByPaddingToLength:add withString: paddingCharString startingAtIndex:0];
    return [pad stringByAppendingString:originalString];
}
else
    return originalString;
}
Community
  • 1
  • 1
d3vinda
  • 1
  • 1
  • `- (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;` – Alex Zavatone Oct 13 '17 at 19:17