0
votes

I get the response from service url and I took them into array.I have the objects in this array with todays week day.But while doing so I get commas from service.So I need to remove the commas from them and compare.How can I do it?

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

So how can I remove that extra comma when there are two commas and compare:

2

2 Answers

1
votes

Try this,

  for (NSDictionary *dict in array3) {

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

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

    [yourArray addObject:newstr];

}
1
votes

Use stringByReplacingOccurrencesOfString like this:

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:@","]:
}

EDIT : you can also use replaceObjectAtIndex:withObject: