How to separate numbers from a string?
I have a string coming from database in a format like
"2,5,5开发者_如何学C,3"
. What i am trying to do is, i want to separate every int from this string and assign in a new variable. Than, on the basis if it i want to show images. For ex: i have 5 image views named as
img1,img2,img3 etc
so if i get output from db as "3,2,4 "
than i want to show img3, img2 and img4. So my images depends on the returned string. How can i move forward in order to implement it.
You can use
NSArray *yourNumbers = [yourString componentsSeparatedByString:@","];
for (NSString* aNumber in yourNumbers){
NSString* anImage = [NSString stringWithFormat:@"img%@",aNumber];
...
}
Use like this
img1.hidden=YES;
img2.hidden=YES;
img3.hidden=YES;
img4.hidden=YES;
img5.hidden=YES;
NSArray *array = [yourString componentsSeparatedByString:@","];
for (NSString* aNumber in array)
{
NSInteger i=[aNumber intValue];
switch(i)
{
case 1:
img1.hidden=NO;
break;
case 2:
img2.hidden=NO;
break;
case 3:
img3.hidden=NO;
break;
case 4:
img4.hidden=NO;
break;
case 5:
img5.hidden=NO;
break;
}
}
精彩评论