Character occurrences in a String Objective C

后端 未结 7 1528
生来不讨喜
生来不讨喜 2021-02-19 19:01

How can I count the occurrence of a character in a string?

Example

String: 123-456-7890

I want to find the occurrence count of \"-\

相关标签:
7条回答
  • 2021-02-19 20:01
    int total = 0;
    NSString *str = @"123-456-7890";
    for(int i=0; i<[str length];i++)
    {
        unichar c = [str characterAtIndex:i];
        if (![[NSCharacterSet alphanumericCharacterSet] characterIsMember:c])
        {
            NSLog(@"%c",c);
            total++;
        }
    }
    NSLog(@"%d",total);
    

    this worked. hope it helps. happy coding :)

    0 讨论(0)
提交回复
热议问题