How to split items in a string separated by “,”

后端 未结 6 1417
栀梦
栀梦 2021-01-03 02:33

In my App, data comes in String like this

\"Hi,Hello,Bye\"

I want to separate data by \",\"

How can I do that?

相关标签:
6条回答
  • 2021-01-03 03:13

    Well, the naïve approach would be to use componentsSeparatedByString:, as suggested in the other answers.

    However, if your data is truly in the CSV format, you'd do well to consider using a proper CSV parser, such as this one (which I wrote): https://github.com/davedelong/CHCSVParser

    0 讨论(0)
  • 2021-01-03 03:17

    Use [myString componentsSeparatedByString:@","].

    0 讨论(0)
  • 2021-01-03 03:26
    NSArray *components = [@"Hi,Hello,Bye" componentsSeparatedByString:@","];
    

    Apple's String Programming Guide will help you get up to speed.

    0 讨论(0)
  • 2021-01-03 03:34

    use componentsSeparatedByString:

    NSString *str = @"Hi,Hello,Bye";  
    NSArray *arr = [str componentsSeparatedByString:@","];  
    NSString *strHi = [arr objectAtIndex:0];  
    NSString *strHello = [arr objectAtIndex:1];
    NSString *strBye = [arr objectAtIndex:2];
    
    0 讨论(0)
  • 2021-01-03 03:34

    If it's NSString, you can use componentsSeparatedByString.

    If it's std::string, you can iterate looking for the item (using find_frst_of and substr)

    0 讨论(0)
  • 2021-01-03 03:37
    NSString *str = @"Hi,Hello,Bye";
    
    NSArray *aArray = [str componentsSeparatedByString:@","];
    

    For more info, look at this post.

    0 讨论(0)
提交回复
热议问题