How to convert string to array in objective C?

前端 未结 5 1197
不思量自难忘°
不思量自难忘° 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:38

    try this

    NSString *str2=@"Hi,How r u"; 
        NSMutableArray *arary = [[NSMutableArray alloc] initWithArray:[str2 componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]]];
        NSLog(@"%@",arary);
    

    if you want , as a object

    NSString *str2=@"Hi,How r u";
        str2 = [str2 stringByReplacingOccurrencesOfString:@"," withString:@" , "];
        NSMutableArray *arary = [[NSMutableArray alloc] initWithArray:[str2 componentsSeparatedByString:@" "]];
        NSLog(@"%@",arary);
    

提交回复
热议问题