Shortcut to generate an NSRange for entire length of NSString?

Basil Bourque picture Basil Bourque · Feb 25, 2013 · Viewed 32.7k times · Source

Is there a short way to say "entire string" rather than typing out:

NSMakeRange(0, myString.length)]

It seems silly that the longest part of this kind of code is the least important (because I usually want to search/replace within entire string)…

[myString replaceOccurrencesOfString:@"replace_me"
                          withString:replacementString
                             options:NSCaseInsensitiveSearch
                               range:NSMakeRange(0, myString.length)];

Answer

jscs picture jscs · Feb 25, 2013

Function? Category method?

- (NSRange)fullRange
{
    return (NSRange){0, [self length]};
}

[myString replaceOccurrencesOfString:@"replace_me"
                          withString:replacementString
                             options:NSCaseInsensitiveSearch
                               range:[myString fullRange]];