字符串目标C中的字符出现

11 iphone objective-c nsstring ipad

如何计算字符串中字符的出现次数?

字符串:123-456-7890

我想在给定的字符串中找到" - "的出现次数

Jus*_*Boo 34

你可以这样做:

NSString *string = @"123-456-7890";
int times = [[string componentsSeparatedByString:@"-"] count]-1;

NSLog(@"Counted times: %i", times);
Run Code Online (Sandbox Code Playgroud)

输出:

Counted times: 2

  • @Oscar它仍然可以工作,我只是测试它.你自己尝试过吗? (3认同)