2 mapmapm MapMapM 于 2012.12.28 10:16 提问

从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,
)

3个回答

prettYYoyic
prettYYoyic   2012.12.28 11:12
已采纳

试试:

  for (NSDictionary *dict in array3) {

             NSString *str=[dict valueForKey:@"Code"];

  NSString*newstr=  [str stringByReplacingOccurrencesOfString:@"," withString:@""];

    [yourArray addObject:newstr];

}
a21064346
a21064346   2012.12.28 10:38

http://ask.csdn.net/questions/927#answer_1629

这里 有个类似的问题,刚解答不久- -!希望对你游泳

gaoXxxing
gaoXxxing 这两个实现内容不一样的啊
接近 5 年之前 回复
gaoXxxing
gaoXxxing   2012.12.28 12:54

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:

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!