开发者

pasing complex string on iphone

i want to parse this string

appDelegate.gpsArray= "\n\n 21.318\n03.863\n\n\n 21.317\n03.864\n\n\n 11.316\n113.864\n\n\n"

so that i cant get value like 1) a[0]=21.318, a[1]=03.863,a[3]=11.317,a[4]=113.846....

or how to get this as 2d array like 2)

a[0,0]=21.318,103.863

a[1,1]=21.318,103.863

i did this but of no use

NSMutableArray  *a1 = [[NSMutableArray alloc开发者_开发百科] init];
        [a1 addObjectsFromArray:appDelegate.gpsArray   ];

Kindly suggest how to use both ways like 1D and 2D


I'd solve your problem in this way:

NSArray *stringsArray = [appDelegate.gpsArray componentsSeparatedByString:"\n"];
NSMutableArray *result = [NSMutableArray array];

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];

for (NSString *str in stringsArray ) {
    if( ![str isEqualToString:@""] ) {
        [result addObject:[f numberFromString:str]];
    }
}

[f release];

Then you can convert the result array to two-dimensional array:

NSMutableArray *result2D = [NSMutableArray array];

for ( int i = 0; i < [result count] / 2; i++ ) {
    NSMutableArray *innerArray = [[NSMutableArray alloc] init];
    [innerArray addObject:[result objectAtIndex:2 * i]];
    [innerArray addObject:[result objectAtIndex:2 * i + 1]];
    [result2D addObject:innerArray];
    [innerArray release];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜