Content from a string into a array
I have a string that needs to be put sorted(not in the high to low way), the string is set to separate each number with a comma and after every third number a space:
26,2496,1492252306 1049,99,68353132 860,99,59587920 4717,99,43863427 807,99,67243688 1382,99,37865117 1088,99,14810482 95,99,67711274 5,99,200000000 3310,99,25639438 75,99,157217334 1923,99,29678736 48,99,118806349 133,99,35055345 563,99,21098356 3410,99,17805574 47,99,62821143 2504,99,14821240 5,99,200000000 3076,99,19425499 474,99,31321799 3983,99,16825678 17826,99,13768252 1129,99,17249030 5393,99,14082021 338,120,137201472 2850,1944 -开发者_如何学C1,-1 -1,-1 -1,-1 -1,-1 4509,3988 3863,3987 2632,4148 5790,4105 -1,-1 -1,-1
i need to create some sort of association for each of the 3 numbers. kinda like a something along the lines of 2d array and a dictionary. any suggestion?
Assuming your string is saved in a variable named "myNumbers", do this:
NSString *myNumbers = @"26,2496,1492252306 1049,99,68353132 860,99,59587920 4717,99,43863427 807,99,67243688 1382,99,37865117 1088,99,14810482 95,99,67711274 5,99,200000000 3310,99,25639438 75,99,157217334 1923,99,29678736 48,99,118806349 133,99,35055345 563,99,21098356 3410,99,17805574 47,99,62821143 2504,99,14821240 5,99,200000000 3076,99,19425499 474,99,31321799 3983,99,16825678 17826,99,13768252 1129,99,17249030 5393,99,14082021 338,120,137201472 2850,1944 -1,-1 -1,-1 -1,-1 -1,-1 4509,3988 3863,3987 2632,4148 5790,4105 -1,-1 -1,-1";
NSArray *triples = [myNumbers componentsSeparatedByString:@" "];
NSMutableArray *matrix = [[NSMutableArray alloc] init];
for (NSString *triple in triples) {
[matrix addObject:[triple componentsSeparatedByString:@","]];
}
NSLog(@"matrix: %@", matrix);
[matrix release];
How about split the string with white space first, then split each of the splited items with comma like following algorithm?
threeItems = split main string with white space
for each itemString in threeItems
threeItem = split itemString with comma
do something with threeItem
end for
精彩评论