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
Comments
-
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 over 11 yearsHah. Awesome that we both named it
fullRange
, and that we both used the new compound literals, at the same time. -
jscs over 11 yearsGeez, we even gave it the same name.
-
jscs over 11 yearsIndeed. And commented at the same time!
-
Basil Bourque over 11 yearsClever. 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 over 11 yearsAnd you are right, sometimes a little bit more of code makes it more readable and efficient.
-
Shinigami about 10 yearsI used this as well and ran into a problem: if your string happens to be empty (@""), you get {NSNotFound, 0}.
-
Ossir over 9 yearsThis function only parse valid ranges as
@"{0, 13}"
etc, it will not createNSRange
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 almost 9 yearsThe literals work just fine, but you could also do return NSMakeRange(0, [self length]); if you wanted.
-
SwiftArchitect over 7 yearsCorrect. But the question is "Shortcut to generate an NSRange for entire length of NSString"
-
Tim Vermeulen almost 7 yearsThe question is also about Objective-C. If you use Swift, then also use String.
-
ma11hew28 almost 7 yearsFor
"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 over 5 years
NSString
may be obsolete in Swift, butNSAttributedString
isn't, and the same code works for it (aside from the class name, obviously).