当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - Transforming NSMutableArray values

问题描述:

I've been looking for this, but can't find the answer.

I have an NSMutableArray with values who_1, what_2, where_3 etc.

I want to transform this into who, what, where etc.

I already have the value of the integer as a variable, and _ is just a string.

What steps should I take to have all these arrayvalues transformed?

网友答案:
 NSArray * arrB = [[NSArray alloc]initWithObjects:@"apple_a",@"ball_b",@"cat_c",@"doll_d",nil]; 

 NSMutableArray * arrA = [[NSMutableArray alloc]init];


for(NSString *strData in arrB)
    {
        NSArray *arr = [strData componentsSeparatedByString:@"_"];
        [arrA  addObject:[arr objectAtIndex:0]];
    } 

and this would be your output

arrA:(
    apple,
    ball,
    cat,
    doll
)
网友答案:

You need to apply logic for that, You cant find answers to tricky Questions :) You need to run a loop.

Separate string with '_'

Loop

for(NSString *s in ary)
{
   NSArray *a = [s componentsSeparatedByString:@"_"];
   [anotherArray addObject:[a objectAtIndex:0]];
}

and update your array..

网友答案:

Following might help you -

NSRange range = [string rangeOfString:@"_"]; 
NSString *finalString = [originalString substringToIndex:range.location];

you can have this in loop.

Or you can go for componentSeperatedByStrings.

网友答案:

This might help you

NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
    NSCharacterSet  *charSet  = [NSCharacterSet characterSetWithCharactersInString:@"_0123456789"];
    _string =  [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
    [tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(@"%@", tmpAry); // Gives the modified array
[tmpAry release];
分享给朋友:
您可能感兴趣的文章:
随机阅读: