0

我从服务 url 得到响应并将它们放入数组中。我在今天的工作日中有这个数组中的对象。但是这样做时我从服务中得到逗号。所以我需要从它们中删除逗号并进行比较。如何我做吗?

array3:
(
{
    Code = "Sunday,";
},
{
    Code = "Thursday,";
},
{
    Code = "Friday,";
},
{
    Code = "Saturday,";
}
)

To compare:

  NSMutableArray *yourArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in array3) {
        [yourArray addObject:[dict valueForKey:@"Code"]];

    }
    BOOL isTheObjectThere = [yourArray containsObject:@"Thursday"];


But I get isTheObjectThere as 'NO' because yourArray is like:

(
Sunday,,
Thursday,,
Friday,,
Saturday,
)

那么,当有两个逗号并进行比较时,如何删除多余的逗号:

4

2 回答 2

1

尝试这个,

  for (NSDictionary *dict in array3) {

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

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

    [yourArray addObject:newstr];

}
于 2012-12-27T11:47:00.380 回答
1

像这样使用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:

于 2012-12-27T11:48:46.060 回答