如何将数字123转换为:“00 \ 00 \ 00 \ 12 \ 3”? stringwithformat可以这样做吗?
答案 0 :(得分:1)
我发现之前的答案更难以阅读,所以这里的版本比其他人建议的更简单:
NSString *stringByAddingSlashesToNumber(int num) {
NSMutableString *str = [NSMutableString stringWithFormat:@"%09i", num];
// we start at index 2 to skip the first pair, and increment by 3
// because we need to skip the two digits and the added backslash
for (NSUInteger idx = 2; idx < str.length; idx += 3) {
[str insertString:@"\\" atIndex:idx];
}
return [str copy];
}
希望逻辑更容易理解,并按照您的期望行事。