计算之间的逗号

发布于 2024-12-10 13:17:13 字数 1245 浏览 0 评论 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 = ???;
           }  

我该怎么办?

I'm feeling noob. How do I count commas? I don't know how to do it.
I want code that look something like this.

Label.text = found 4 commas!!

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

Label.text = found 3 commas!!

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

label.text = found 1 comma!!

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

Entering noobie textbox ["343,433,463"] and I should have 2 commas.

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

How do I this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一口甜 2024-12-17 13:17:13
NSArray * foo = [str componentsSeparatedByString:@","];

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

label.text = [NSString stringWithFormat:@"Found %d commas", [foo count] -1];
桃扇骨 2024-12-17 13:17:13

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

NSString componentsSeparatedByCharactersInSet: or componentsSeparatedByString: and get the size of the array returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文