Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SubStrings from String Array iPhone

I have an array, see below,

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

Now i want all the string which is right to the character "x" in to another array. That is i need an array with elements @"2",@"3",@"3" from the above array.

How can i achieve this?? thanks..

like image 802
Muhammed Sadiq.HS Avatar asked Sep 02 '11 11:09

Muhammed Sadiq.HS


2 Answers

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]];
}
like image 140
Robin Avatar answered Oct 05 '22 10:10

Robin


    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];
    }
like image 27
Nekto Avatar answered Oct 05 '22 08:10

Nekto