【问题标题】:How to split items in a string separated by ","如何拆分以“,”分隔的字符串中的项目
【发布时间】:2011-10-18 19:47:56
【问题描述】:

在我的应用程序中,数据以这样的字符串形式出现

"Hi,Hello,Bye"

我想用“,”分隔数据

我该怎么做?

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4


    【解决方案1】:

    使用[myString componentsSeparatedByString:@","]

    【讨论】:

      【解决方案2】:
      NSString *str = @"Hi,Hello,Bye";
      
      NSArray *aArray = [str componentsSeparatedByString:@","];
      

      欲了解更多信息,请查看此post

      【讨论】:

        【解决方案3】:

        如果是NSString,可以使用componentsSeparatedByString

        如果是 std::string,您可以迭代查找该项目(使用 find_frst_ofsubstr

        【讨论】:

          【解决方案4】:
          NSArray *components = [@"Hi,Hello,Bye" componentsSeparatedByString:@","];
          

          Apple's String Programming Guide 将帮助您快速上手。

          【讨论】:

            【解决方案5】:

            好吧,天真的方法是使用 componentsSeparatedByString:,正如其他答案中所建议的那样。

            但是,如果您的数据确实是 CSV 格式,那么您最好考虑使用适当的 CSV 解析器,例如这个(我写的):https://github.com/davedelong/CHCSVParser

            【讨论】:

            • CSV 示例将来会很有用。谢谢
            【解决方案6】:

            使用 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];
            

            【讨论】:

            • 这是不正确的。下面的答案中注明了正确的语法:[str componentsSeparatedByString:@","];
            猜你喜欢
            • 1970-01-01
            • 2014-06-29
            • 2012-06-24
            • 1970-01-01
            • 2011-03-24
            • 1970-01-01
            • 1970-01-01
            • 2012-05-24
            相关资源
            最近更新 更多