String Array iPhone的SubStrings

时间:2011-09-02 11:57:38

标签: iphone objective-c ios

我有一个数组,见下文,

NSMutableArray *myArray=[[NSMutableArray alloc]initWithObjects:@"45 x 2",@"76 x 3",@"98 x 3", nil];      

现在我希望所有字符串“x”的字符串都在另一个数组中。那就是我需要一个包含上面数组中元素@"2",@"3",@"3"的数组。

我怎样才能实现这一目标? 感谢..

3 个答案:

答案 0 :(得分:12)

NSMutableArray *myArray=[[NSMutableArray alloc]initWithObjects:@"45 x 2",@"76 x 3",@"98 x 3", nil];
NSMutableArray *tempArray = [NSMutableArray array];
for(NSString *string in myArray)
{
    NSArray *array = [string componentsSeparatedByString:@"x"];
    if(array.count > 1)
        [tempArray addObject:[array objectAtIndex:1]];
}

答案 1 :(得分:4)

    NSMutableArray *myArray=[[NSMutableArray alloc]initWithObjects:@"45 x 2",@"76 x 3",@"98 x 3", nil];
    NSMutableArray *suffixArray = [NSMutableArray array];
    for (NSString *el in myArray)
    {
        NSRange range = [el rangeOfString:@"x"];
        if (range.location == NSNotFound) [prefixArray addObject:@""];
        NSString *suffix = [el substringFromIndex:range.location+range.length];
        [suffixArray addObject:suffix];
    }

答案 2 :(得分:0)

迭代for循环并实现此

NSArray *splitedString = [[myArray objectAtIndex:indexVal] componentsSeparatedByString: @"x"];


    if ([splitedString count]>1)
{
            NSString *prefixString=[splitedString objectAtIndex:1];
            //Add this to ur new array
}