objective-c code to right pad a NSString?

前端 未结 6 1349
死守一世寂寞
死守一世寂寞 2020-12-06 01:26

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          


        
相关标签:
6条回答
  • 2020-12-06 01:49

    Try below. its working for me.

    NSString *someString = @"1234";
    NSString *padded = [someString stringByPaddingToLength: 16 withString: @"x" startingAtIndex:0];
    
    NSLog(@"%@", someString);
    NSLog(@"%@", padded);
    
    0 讨论(0)
  • 2020-12-06 01:52

    %-@ 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].......

    0 讨论(0)
  • 2020-12-06 02:02

    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.

    0 讨论(0)
  • 2020-12-06 02:04

    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];
    
    0 讨论(0)
  • 2020-12-06 02:06

    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.

    0 讨论(0)
  • 2020-12-06 02:15

    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];
    
    0 讨论(0)
提交回复
热议问题