0

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?

1
  • 1
    I would split it into an array of characters and then join it with the comma as a separator. In Swift it's a one-liner: Array(zipcode).map(String.init).joined(separator:",") Commented Jun 15, 2018 at 15:10

1 Answer 1

2

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:@","];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! It worked pretty well although I agree Objective C's solution is way more awful than Swift's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.