Search code examples
objective-ccocoa-touchstring-formatting

How do I format a String to credit card format?


What is the easiest way to format a string like "1234567890123456789" to "1234 5678 9012 3456 789" in iOS?


Solution

  • Try this:

    -(NSString *) correctString:(NSString *) anyStr {
        NSMutableString *str = [NSMutableString stringWithString:anyStr];
        int indx=4;
        while (indx<str.length) {
            [str insertString:@" " atIndex:indx];
            indx +=5;
        }
        anyStr=str;
        return anyStr;
    }