0

I have string as below.

imageFinalNames = @"car1389188719-1224596-22.jpg,car1389188659-1224536-22.jpg,car1389188311-1224187-22.jpg,car1389187911-1223788-22.jpg,";

I want to print all those names separately.

I want result as below.

car1389188719-1224596-22.jpg
car1389188659-1224536-22.jpg
car1389188311-1224187-22.jpg
car1389187911-1223788-22.jpg

Currently what I am doing is using for loop and finding the location of comma and finding substring accordingly and then print it.

I know this is UGLY WAY

Is there any easy and efficient way to get what I wanted?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • 3
    See `-componentsSeparatedByString:` duplicate of : http://stackoverflow.com/questions/6922131/how-to-split-items-in-a-string-separated-by – Emmanuel Jan 08 '14 at 14:55

2 Answers2

3

You can use

NSString *imageFinalNames = @"car1389188719-1224596-22.jpg,car1389188659-1224536-22.jpg,car1389188311-1224187-22.jpg,car1389187911-1223788-22.jpg,";

NSArray *array = [testString componentsSeparatedByString:@","];

In this array you will find all words. Careful: you have at end a comma and you will have in the array a last item that won't contain anything.

Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165
Raluca Lucaci
  • 2,058
  • 3
  • 20
  • 37
0

Below is how I did...

imageFinalNames.text = @"car1389188719-1224596-22.jpg,car1389188659-1224536-22.jpg,car1389188311-1224187-22.jpg,car1389187911-1223788-22.jpg,";

int times = [[imageFinalNames.text componentsSeparatedByString:@","] count]-1;

NSLog(@"times==%d", times);

NSArray *parts = [imageFinalNames.text componentsSeparatedByString:@","];

int i;
NSString *myStr ;
for (i=0;i<times;i++) {

    myStr = [parts objectAtIndex:i];

    NSLog(@"i==%d--myStr==%@",i, myStr);
}
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276