Octavio Rojas
Octavio Rojas

Reputation: 187

Inserting a comma after every character in a string? Objective C

Hello I'm receiving a string that contains a zipcode. E.G. "34747" and I need to convert it to something like this "3,4,7,4,7"

What would be the best way to implement this in Objective C?

Upvotes: 0

Views: 157

Answers (1)

Alper
Alper

Reputation: 3953

In Objective-C it's pretty awful:

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];

for (int i=0; i < [myString length]; i++) {
    NSString *ichar  = [NSString stringWithFormat:@"%C", [myString characterAtIndex:i]];
    [characters addObject:ichar];
}

NSString *output = [characters componentsJoinedByString:@","];

Upvotes: 2

Related Questions