0

我感觉菜鸟 如何计算逗号?我不知道该怎么做。我想要看起来像这样的代码。

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 = ???;
           }  

我该怎么做?

4

2 回答 2

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

label.text = [NSString stringWithFormat:@"Found %d commas", [foo count] -1];
于 2011-10-17T19:32:21.747 回答
0

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

于 2011-10-17T19:32:41.043 回答