Shortcut to generate an NSRange for entire length of NSString?

35,460

Solution 1

Function? Category method?

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

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

Solution 2

Swift 4+, useful for NSRegularExpression and NSAttributedString

extension String {
    var nsRange : NSRange {
        return NSRange(self.startIndex..., in: self)
    }

    func range(from nsRange: NSRange) -> Range<String.Index>? {
        return Range(nsRange, in: self)
    }
}

Solution 3

Not that I know of. But you could easily add an NSString category:

@interface NSString (MyRangeExtensions)
- (NSRange)fullRange
@end

@implementation NSString (MyRangeExtensions)
- (NSRange)fullRange {
  return (NSRange){0, self.length};
}

Solution 4

Swift

NSMakeRange(0, str.length)

or as an extension:

extension NSString {
    func fullrange() -> NSRange {
        return NSMakeRange(0, self.length)
    }
}

Solution 5

Swift 2:

extension String {
    var fullRange:Range<String.Index> { return startIndex..<endIndex }
}

as in

let swiftRange = "abc".fullRange

or

let nsRange = "abc".fullRange.toRange
Share:
35,460
Basil Bourque
Author by

Basil Bourque

SOreadytohelp

Updated on June 17, 2020

Comments

  • Basil Bourque
    Basil Bourque almost 4 years

    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)];
    
  • BJ Homer
    BJ Homer over 11 years
    Hah. Awesome that we both named it fullRange, and that we both used the new compound literals, at the same time.
  • jscs
    jscs over 11 years
    Geez, we even gave it the same name.
  • jscs
    jscs over 11 years
    Indeed. And commented at the same time!
  • Basil Bourque
    Basil Bourque over 11 years
    Clever. But I hesitate to introduce what could be a lot of CPU work (matching a long string against itself) for the relatively small benefit of a little less typing for me the programmer.
  • Odrakir
    Odrakir over 11 years
    And you are right, sometimes a little bit more of code makes it more readable and efficient.
  • Shinigami
    Shinigami about 10 years
    I used this as well and ran into a problem: if your string happens to be empty (@""), you get {NSNotFound, 0}.
  • Ossir
    Ossir over 9 years
    This function only parse valid ranges as @"{0, 13}" etc, it will not create NSRange for full length of string, if string does not contain valid range, but rather random string, it returns {0,0} range. nshipster.com/nsrange
  • Brian Sachetta
    Brian Sachetta almost 9 years
    The literals work just fine, but you could also do return NSMakeRange(0, [self length]); if you wanted.
  • SwiftArchitect
    SwiftArchitect over 7 years
    Correct. But the question is "Shortcut to generate an NSRange for entire length of NSString"
  • Tim Vermeulen
    Tim Vermeulen almost 7 years
    The question is also about Objective-C. If you use Swift, then also use String.
  • ma11hew28
    ma11hew28 almost 7 years
    For "abc".fullRange.toRange, I get the compile time error: "Value of type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>') has no member 'toRange'." I'm running Xcode 8.3.3 with Swift 3.1.
  • John Montgomery
    John Montgomery over 5 years
    NSString may be obsolete in Swift, but NSAttributedString isn't, and the same code works for it (aside from the class name, obviously).