objective-c code to right pad a NSString?

前端 未结 6 1359
死守一世寂寞
死守一世寂寞 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: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].......

提交回复
热议问题