objective-c Padding string to left

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/964322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:41:20  来源:igfitidea点击:

Padding string to left

objective-ciphonexcode

提问by TonyNeallon

How can one pad a string to left in objective c. E.g if I have an integer value of 6 I want it to be displayed as 06.

How can one pad a string to left in objective c. E.g if I have an integer value of 6 I want it to be displayed as 06.

I use stringByPaddingToLength:but it pads it to the right like 60.

I use stringByPaddingToLength:but it pads it to the right like 60.

Your help is much appreciated.

Your help is much appreciated.

采纳答案by aimless

+ (NSString *)formatValue:(int)value forDigits:(int)zeros {
    NSString *format = [NSString stringWithFormat:@"%%0%dd", zeros]; 
    return [NSString stringWithFormat:format,value];
} 

回答by nevan king

This one pads left with 10 zeroes.

This one pads left with 10 zeroes.

NSString *padded = [NSString stringWithFormat:@"Padded left with zeros: %010d", 65];

回答by dreamlax

Clumsy, but it'll do the job.

Clumsy, but it'll do the job.

@implementation NSString (LeftPadding)

- (NSString *)stringByPaddingTheLeftToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex
{
    if ([self length] <= newLength)
        return [[@"" stringByPaddingToLength:newLength - [self length] withString:padString startingAtIndex:padIndex] stringByAppendingString:self];
    else
        return [[self copy] autorelease];
}

@end

Then you can do:

Then you can do:

NSString *test1 = [@"6" stringByPaddingTheLeftToLength:10 withString:@"0" startingAtIndex:0];
// test1 = "0000000006"

NSString *test2 = [@"asdf" stringByPaddingTheLeftToLength:10 withString:@"qwer" startingAtIndex:0];
// test2 = "qwerqwasdf"

NSString *test3 = [@"More than ten characters" stringByPaddingTheLeftToLength:10 withString:@"bamboo" startingAtIndex:0];
// test3 = "More than ten characters"

NSString *test4 = [test3 stringByPaddingTheLeftToLength:100 withString:test2 startingAtIndex:0];
// test4 = "qwerqwasdfqwerqwasdfqwerqwasdf...qwerqMore than ten characters"

回答by bmauter

Old thread, I know, but this is a handy way to left pad. I have it my Util class. You can adapt it to right pad if needed. The best part is that it works with whatever padding you want.

Old thread, I know, but this is a handy way to left pad. I have it my Util class. You can adapt it to right pad if needed. The best part is that it works with whatever padding you want.

+ (NSString *) leftPadString:(NSString *)s withPadding:(NSString *)padding {
    NSString *padded = [padding stringByAppendingString:s];
    return [padded substringFromIndex:[padded length] - [padding length]];
}

ARC and non-ARC compliant. :)

ARC and non-ARC compliant. :)

To call it, use [Util leftPadString:@"42" withPadding:@"0000"];.

To call it, use [Util leftPadString:@"42" withPadding:@"0000"];.

You could also put it in a category on NSString for an even easier call like [@"42" stringByLeftPadding:@"0000"].

You could also put it in a category on NSString for an even easier call like [@"42" stringByLeftPadding:@"0000"].

Both give you 0042.

Both give you 0042.

回答by BananaAcid

for completness (using *placeholder):

for completness (using *placeholder):

NSString *paddedNumberString = [NSString stringWithFormat:@"%0*d", amountOfZeros, numberNSInteger]