2011-10-17 55 views
0

我感覺到noob。我如何計算逗號?我不知道該怎麼做。 我想要看起來像這樣的代碼。計數逗號在

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文本框[「343433463」],我應該有2個逗號。

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

我該怎麼做?

回答

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

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

我得到了一個錯誤。它表示期望label.text = [[NSString stringWithFormat:@「Found%d commas」,[foo count] -1]; < - 顯示紅色。 – user971636

+0

是的,我有一個太多[[,我編輯過。 –

0

NSStringcomponentsSeparatedByCharactersInSet:componentsSeparatedByString:並獲得數組的大小返回。