簡體   English   中英

計算中間的逗號

[英]Counting commas in between

我感覺菜鳥如何計算逗號? 我不知道該怎么做。 我想要看起來像這樣的代碼。

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

我該怎么做?

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

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

NSString componentsSeparatedByCharactersInSet:componentsSeparatedByString:並獲取返回數組的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM