[Solved] Insert “:”after 2 string in Cocoa [closed]


You will need to calculate how many two-character pairs there are. From there, you can loop through the pairs and grab the substring from the original string, and start stitching together the new string with colons in between. You need to leave the colon off for the last pair.

Here’s some code below, this assumes that the original string has an even number of characters for complete pairs.

NSString *originalString = @"000C290C16E8";

NSMutableString *deliniatedString = [NSMutableString string];

NSInteger octetCount = [originalString length] / 2;

for (NSInteger i = 0; i < octetCount; i++)
{
    NSString *substring = [originalString substringWithRange:NSMakeRange(i * 2, 2)];
    [deliniatedString appendString:substring];

    if (i < octetCount - 1)
        [deliniatedString appendString:@":"];
}

NSLog(@"%@", deliniatedString);

0

solved Insert “:”after 2 string in Cocoa [closed]