Mask for CNPJ in Objective C

1

I'm trying to put a mask on a string that is a CNPJ. I get a string with size 14 and tried to put the mask in:

 NSString *cnpj = [NSString stringWithFormat:@"%@.%@.%@/%@-%@", 
 self.codCnpj substringWithRange:NSMakeRange(11, 2)], 
 [self.codCnpj substringWithRange:NSMakeRange(8, 3)], 
 [self.codCnpj substringWithRange:NSMakeRange(5, 3)],     
 [self.codCnpj substringWithRange:NSMakeRange(1, 4)],
 [self.codCnpj substringWithRange:NSMakeRange(0, 2)]];

Well, that is not giving a damn right .. Some values repeat themselves and others are not shown. Does anyone have any suggestions for improvement or a simpler way to do this?

    
asked by anonymous 09.09.2014 / 18:24

1 answer

2

I'm going to respond because I found a form only using substring myself. What was wrong was the order of the indices. Then follow my solution:

NSString *finalCnpj = [NSString stringWithFormat:@"%@.%@.%@/%@-%@", 
[self.cnpj substringWithRange:NSMakeRange(0, 2)], 
[self.cnpj substringWithRange:NSMakeRange(2, 3)], 
[self.cnpj substringWithRange:NSMakeRange(5, 3)], 
[self.cnpj substringWithRange:NSMakeRange(8, 4)], 
[self.cnpj substringWithRange:NSMakeRange(12, 2)]];
    
09.09.2014 / 19:25