问题描述
- 从NSMutablearray中删除逗号
-
我从服务器URL获取的结果放到数组中,数组中放了星期对象,但是在获取的时候会得到逗号,我想删除这些逗号然后可以比较。不知道应该怎么做?array3: ( { Code = "Sunday,"; }, { Code = "Thursday,"; }, { Code = "Friday,"; }, { Code = "Saturday,"; } )
比较:
NSMutableArray *yourArray = [[NSMutableArray alloc] init]; for (NSDictionary *dict in array3) { [yourArray addObject:[dict valueForKey:@"Code"]]; } BOOL isTheObjectThere = [yourArray containsObject:@"Thursday"];
但是系统会报出isTheObjectThere 'NO' 错误因为得到数组如下:
( Sunday,, Thursday,, Friday,, Saturday, )
解决方案
试试:
for (NSDictionary *dict in array3) {
NSString *str=[dict valueForKey:@"Code"];
NSString*newstr= [str stringByReplacingOccurrencesOfString:@"," withString:@""];
[yourArray addObject:newstr];
}
解决方案二:
http://ask.csdn.net/questions/927#answer_1629
这里 有个类似的问题,刚解答不久- -!希望对你游泳
解决方案三:
用stringByReplacingOccurrencesOfString
:
NSMutableArray *newArrayDays = [NSMutableArray array];
for(int i=0; i< [mutArrDays count]; i++) //your array of days here to remove commas from it
{
[newArrayDays addObject:[[mutArrDays objectAtIndex:i]stringByReplacingOccurrencesOfString@",," withString:@","]:
}
另外你还可以用replaceObjectAtIndex:withObject
:
时间: 2025-01-19 14:24:24