Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top