72

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)];
Arun_
  • 1,806
  • 2
  • 20
  • 40
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • If you need more than 5 times, then you can make a method/category, But for under 5 time your `NSMakeRange(0, myString.length)]` will be shorter, isn't it? – Anoop Vaidya Feb 25 '13 at 08:08

6 Answers6

47

Function? Category method?

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

[myString replaceOccurrencesOfString:@"replace_me"
                          withString:replacementString
                             options:NSCaseInsensitiveSearch
                               range:[myString fullRange]];
jscs
  • 63,694
  • 13
  • 151
  • 195
28

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)
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
20

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};
}
BJ Homer
  • 48,806
  • 11
  • 116
  • 129
16

Swift

NSMakeRange(0, str.length)

or as an extension:

extension NSString {
    func fullrange() -> NSRange {
        return NSMakeRange(0, self.length)
    }
}
Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
7

Swift 2:

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

as in

let swiftRange = "abc".fullRange

or

let nsRange = "abc".fullRange.toRange
Community
  • 1
  • 1
Chris Conover
  • 8,889
  • 5
  • 52
  • 68
  • 1
    For `"abc".fullRange.toRange`, I get the compile time error: "Value of type 'Range' (aka 'Range') has no member 'toRange'." I'm running Xcode 8.3.3 with Swift 3.1. – ma11hew28 Aug 12 '17 at 15:57
6

This is not shorter, but... Oh well

NSRange range = [str rangeOfString:str];
Odrakir
  • 4,254
  • 1
  • 20
  • 52
  • 7
    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. – Basil Bourque Feb 25 '13 at 08:19
  • And you are right, sometimes a little bit more of code makes it more readable and efficient. – Odrakir Feb 25 '13 at 08:30
  • I used this as well and ran into a problem: if your string happens to be empty (@""), you get {NSNotFound, 0}. – Shinigami May 08 '14 at 12:52
  • 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. http://nshipster.com/nsrange/ – Ossir Feb 06 '15 at 12:26