0

I am getting Json response from server.

       type = 2;
      "daysofWeek" = "(\n    Mon,\n    Tue\n)";
       serviceType = 2;
       startDate = "2013-10-28";

In above format daysofWeek is Array string. I am trying to convert into NSMutableArray as

  NSString *weekDaysStr=[valueDict objectForKey:@"recrWeek_daysofWeek"];
  NSMutableArray *weekDays=[[NSMutableArray alloc] initWithArray:[weekDaysStr componentsSeparatedByString:@","]];

But when i log this array i shownig as

     ("\n    Mon",
      "\n    Tue\n"
     )

How to remove those extra words from array.

I have check each values to week day.

               NSString *day=@"Mon";
               if([day isEqualToString:[weekDays objectAtIndex:0]){

                }

I tried this type also

       weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"(\n" withString:@""];                      
       weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n)" withString:@""];
    weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n " withString:@""];
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""];                                   
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\"" withString:@""]; 
     weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""];

it showing as

        "Mon",
        Tue

At that time its giving false condtion.Help me on this problem

2 Answers 2

1

You can try to trim the strings in the array yourself:

for (NSString *myString in weekDays)
{
    myString = [myString stringByReplacingOccurrencesOfString: @" " withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"\n" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @"(" withString:@""];
    myString = [myString stringByReplacingOccurrencesOfString: @")" withString:@""];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes i tried . weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"(\n" withString:@""]; weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n)" withString:@""]; weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\n " withString:@""]; weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""]; weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@"\"" withString:@""]; weekDaysStr=[weekDaysStr stringByReplacingOccurrencesOfString:@" " withString:@""]; But i showing comments for first element
like "Mon",Tue this
i Tried.in nslog week days::( "(Mon", "Tue)" ) displaying
1

Try this it will help

NSString *str=@"(\n    Mon,\n    Tue\n)";
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"\n)" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@")" withString:@""];
NSMutableArray *weekDays=[[NSMutableArray alloc] initWithArray:[str componentsSeparatedByString:@","]];
NSLog(@"%@",weekDays );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.