How to convert string to array in objective C?

前端 未结 5 1196
不思量自难忘°
不思量自难忘° 2021-02-06 08:35

How to convert string to array in objective C. i.e,I have a string,

NSString *str = @\"Hi,How r u\";

This should be converted into an array *NS

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 09:21

    NSString *str=@"Hi,How r u"; 
    NSArray *arr = [str componentsSeparatedByString:@","];
    NSString *strSecond = [arr objectAtIndex:1];
    
    NSMutableArray *arrSecond = [strSecond componentsSeparatedByString:@" "];
    NSString *strHow = [arr objectAtIndex:0];
    NSString *strAre = [arr objectAtIndex:1];
    NSString *strYou = [arr objectAtIndex:2];
    
    [arr removeObjectAtIndex:1];
    [arr addObject:@","];
    [arr addObject:strHow];
    [arr addObject:strAre];
    [arr addObject:strYou];  
    

    arr is the desired array.

提交回复
热议问题