在两者之间计算逗号

时间:2011-10-17 19:20:50

标签: iphone count comma

我感觉不容易。我怎么算逗号?我不知道怎么做。 我希望代码看起来像这样。

Label.text =找到4个逗号!!

          NSString *str = @"100,000,000,000,000";
           NSRange detecting = [str rangeOfString:@","];
            if (detecting .length > 0 ) {
             // Count how many commas?
             // label.text = ???;
           }  

Label.text =找到3个逗号!!

          NSString *str = @"100,000,000,000";
           NSRange detecting = [str rangeOfString:@","];
            if (detecting .length > 0 ) {
             // Count how many commas?
             // label.text = ???;
           }  

label.text =找到1个逗号!!

          NSString *str = @"100,000";
           NSRange detecting = [str rangeOfString:@","];
            if (detecting .length > 0 ) {
             // Count how many commas?
             // label.text = ???;
           }  

输入noobie文本框[“343,433,463”],我应该有2个逗号。

          NSString *str = noobie.text;
           NSRange detecting = [str rangeOfString:@","];
            if (detecting .length > 0 ) {
             // Count how many commas?
             // label.text = ???;
           }  

我该怎么做?

2 个答案:

答案 0 :(得分:2)

NSArray * foo = [str componentsSeparatedByString:@","];

label.text = [NSString stringWithFormat:@"Found %d commas", [foo count] -1];

答案 1 :(得分:0)

NSString componentsSeparatedByCharactersInSet:componentsSeparatedByString:并获取返回的数组大小。