Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3680254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:33:18+00:00 2026-05-19T03:33:18+00:00

I need NSRange objects for the position of each uppercase letter in a given

  • 0

I need NSRange objects for the position of each uppercase letter in a given NSString for input into a method for a custom attributed string class. 

There are of course quite a few ways to accomplish this such as rangeOfString:options: with NSRegularExpressionSearch or using RegexKitLite to get each match separately while walking the string. 

What would be the fastest performing approach to accomplish this task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:33:19+00:00Added an answer on May 19, 2026 at 3:33 am

    The simplest way is probably to use -rangeOfCharacterFromSet:options:range: with [NSCharacterSet uppercaseLetterCharacterSet]. By modifying the range to search over with each call, you can find all of the uppercase letters pretty easily. Something like the following will work to give you an NSArray of all ranges (encoded as NSValues):

    - (NSArray *)rangesOfUppercaseLettersInString:(NSString *)str {
        NSCharacterSet *cs = [NSCharacterSet uppercaseLetterCharacterSet];
        NSMutableArray *results = [NSMutableArray array];
        NSRange searchRange = NSMakeRange(0, [str length]);
        NSRange range;
        while ((range = [str rangeOfCharacterFromSet:cs options:0 range:searchRange]).location != NSNotFound) {
            [results addObject:[NSValue valueWithRange:range]];
            searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
        }
        return results;
    }
    

    Note, this will not coalesce adjacent ranges into a single range, but that’s easy enough to add.

    Here’s an alternative solution based on NSScanner:

    - (NSArray *)rangesOfUppercaseLettersInString:(NSString *)str {
        NSCharacterSet *cs = [NSCharacterSet uppercaseLetterCharacterSet];
        NSMutableArray *results = [NSMutableArray array];
        NSScanner *scanner = [NSScanner scannerWithString:str];
        while (![scanner isAtEnd]) {
            [scanner scanUpToCharactersFromSet:cs intoString:NULL]; // skip non-uppercase characters
            NSString *temp;
            NSUInteger location = [scanner scanLocation];
            if ([scanner scanCharactersFromSet:cs intoString:&temp]) {
                // found one (or more) uppercase characters
                NSRange range = NSMakeRange(location, [temp length]);
                [results addObject:[NSValue valueWithRange:range]];
            }
        }
        return results;
    }
    

    Unlike the last, this one does coalesce adjacent uppercase characters into a single range.

    Edit: If you’re looking for absolute speed, this one will likely be the fastest of the 3 presented here, while still preserving correct unicode support (note, I have not tried compiling this):

    // returns a pointer to an array of NSRanges, and fills in count with the number of ranges
    // the buffer is autoreleased
    - (NSRange *)rangesOfUppercaseLettersInString:(NSString *)string count:(NSUInteger *)count {
        NSMutableData *data = [NSMutableData data];
        NSUInteger numRanges = 0;
        NSUInteger length = [string length];
        unichar *buffer = malloc(sizeof(unichar) * length);
        [string getCharacters:buffer range:NSMakeRange(0, length)];
        NSCharacterSet *cs = [NSCharacterSet uppercaseLetterCharacterSet];
        NSRange range = {NSNotFound, 0};
        for (NSUInteger i = 0; i < length; i++) {
            if ([cs characterIsMember:buffer[i]]) {
                if (range.location == NSNotFound) {
                    range = (NSRange){i, 0};
                }
                range.length++;
            } else if (range.location != NSNotFound) {
                [data appendBytes:&range length:sizeof(range)];
                numRanges++;
                range = (NSRange){NSNotFound, 0};
            }
        }
        if (range.location != NSNotFound) {
            [data appendBytes:&range length:sizeof(range)];
            numRanges++;
        }
        if (count) *count = numRanges;
        return [data bytes];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to locate the following pattern: The letter I followed by a space then
Need a strategy to put Intellij IDEA project files into Git. The main question
i have a input tag which is non editable, but some times i need
i need to validate a textfield input that allows two digits after a .
I need to display 6 views and each view should display 20 items (UIButtons).
Need to turn javascript (ajax) vote script into reusable (dynamic) script so it can
Need some help for creating a File and String search engine. The program needs
Need a function that takes a character as a parameter and returns true if
Need a way to allow sorting except for last item with in a list.
Need to an expression that returns only things with an I followed by either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.