Check if string contains occurence of digit

Teddy13

I would like to check if a NSString contains each digit possible (0-9) more then 5 times. I do not need to know which digit or how many times, I simply want it to return TRUE or False for whether any of the digits are contained more then 5 times in the string. I would like it to be as efficient as possible.

I have given it some thought and the long way of going about it would be to place all 10 digits (again 0-9) in an array and then loop through each digit comparing it to the string. If there are more than 5 matches within the string, place a flag that will return true.

Can anyone tell me if there is a "better" or more efficient way of going about this problem?

Thank you!

Nikolai Ruhe

This code tries to be as performant as possible.

BOOL checkDigits(NSString *string)
{
    // get the raw UTF-16 code fragments, hopefully without a copy
    const UniChar *characters = CFStringGetCharactersPtr((__bridge CFStringRef)string);
    NSData *characterData = nil;
    if (characters == NULL) {
        characterData = [string dataUsingEncoding:NSUTF16StringEncoding];
        characters = [characterData bytes];
    }

    // initialize 10 individual counters for digits
    int digitCount[10] = {};
    NSUInteger length = [string length];

    // loop over the characters once
    for (NSUInteger i = 0; i != length; ++i) {
        UniChar c = characters[i];

        // UTF-16 encodes ASCII digits as their values
        if (c >= '0' && c <= '9') {
            int idx = c - '0';
            if (digitCount[idx] == 4)
                return YES;
            digitCount[idx] += 1;
        }
    }

    // keep the NSData object alive until here
    [characterData self];

    return NO;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check vowels occurence in a string

From Dev

Check if string contains any non digit character - no libraries - Java

From Dev

Regex check to see if a String contains non digit fails

From Dev

Check if a string contains at least a upperCase letter, a digit, or a special character in Swift?

From Dev

Check if string contains a particular digit only (e.g. "111")

From Dev

How to check for occurence of name in string?

From Dev

How to check for occurence of name in string?

From Dev

How can I check (and delete) if the last line (that contains the last occurence of a string) has a comma as last character?

From Dev

Increment 3rd occurence of digit in string with javascript

From Dev

Grails 2.3.8 check url mappings for string occurence

From Dev

Grails 2.3.8 check url mappings for string occurence

From Dev

Check text/string for occurence of predefined list elements

From Dev

Lambda operation on string to check digit

From Dev

Check if String contains the key

From Java

Check if a string contains whitespace

From Java

Check if a string contains a number

From Dev

Check if string contains space

From Dev

Check if string contains a word

From Dev

Check if string contains commas

From Dev

Check if string contains in array

From Dev

Check if String contains alphanumeric

From Dev

Check if a string contains characters

From Dev

Check if a string contains another

From Dev

Check if string contains strictly a string

From Dev

how do i check for a string occurence in input data

From Dev

Check for two decimal digit number in string

From Dev

Check if string contains anything but numbers

From Dev

Check if String contains substring in clojure

From Dev

Check if url contains string with JQuery

Related Related

  1. 1

    Check vowels occurence in a string

  2. 2

    Check if string contains any non digit character - no libraries - Java

  3. 3

    Regex check to see if a String contains non digit fails

  4. 4

    Check if a string contains at least a upperCase letter, a digit, or a special character in Swift?

  5. 5

    Check if string contains a particular digit only (e.g. "111")

  6. 6

    How to check for occurence of name in string?

  7. 7

    How to check for occurence of name in string?

  8. 8

    How can I check (and delete) if the last line (that contains the last occurence of a string) has a comma as last character?

  9. 9

    Increment 3rd occurence of digit in string with javascript

  10. 10

    Grails 2.3.8 check url mappings for string occurence

  11. 11

    Grails 2.3.8 check url mappings for string occurence

  12. 12

    Check text/string for occurence of predefined list elements

  13. 13

    Lambda operation on string to check digit

  14. 14

    Check if String contains the key

  15. 15

    Check if a string contains whitespace

  16. 16

    Check if a string contains a number

  17. 17

    Check if string contains space

  18. 18

    Check if string contains a word

  19. 19

    Check if string contains commas

  20. 20

    Check if string contains in array

  21. 21

    Check if String contains alphanumeric

  22. 22

    Check if a string contains characters

  23. 23

    Check if a string contains another

  24. 24

    Check if string contains strictly a string

  25. 25

    how do i check for a string occurence in input data

  26. 26

    Check for two decimal digit number in string

  27. 27

    Check if string contains anything but numbers

  28. 28

    Check if String contains substring in clojure

  29. 29

    Check if url contains string with JQuery

HotTag

Archive