-4

たとえば、数値 123 を "00\00\00\12\3" に変換するにはどうすればよいですか? stringwithformat はそれを行うことができますか?

4

1 に答える 1

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];
}

うまくいけば、ロジックは従うのが少し簡単になり、期待どおりに機能します。

于 2013-04-11T00:39:29.293 に答える