[NSString rangeOfCharacterFromSet options range]
Finds and returns the range in the receiver of the first character from a given character set found in a given range with given options.
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)aRange
Parameters of [NSString rangeOfCharacterFromSet options range]
- aSet
- A character set. This value must not be
nil. - mask
- A mask specifying search options. The following options may be specified by combining them with the C bitwise
ORoperator:NSCaseInsensitiveSearch,NSLiteralSearch,NSBackwardsSearch. See String Programming Guide for details on these options. - aRange
- The range in which to search. aRange must not exceed the bounds of the receiver.
Return Value of [NSString rangeOfCharacterFromSet options range]
The range in the receiver of the first character found from aSet within aRange. Returns a range of
{NSNotFound, 0} if none of the characters in aSet are found.Discussion of [NSString rangeOfCharacterFromSet options range]
Because pre-composed characters in aSet can match composed character sequences in the receiver, the length of the returned range can be greater than
1. For example, if you search for “ΓΌ” in the string “stru¨del”, the returned range is{3,2}.
This method detects all invalid ranges (including those with negative lengths). For applications linked against OS X v10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution.
[NSString rangeOfCharacterFromSet options range] example
- (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;
}
[NSString rangeOfCharacterFromSet options range] example
NSString * normalize(NSString * number)
{
NSString *normalizedNumber = @"";
NSString *tmpString = nil;
NSRange searchRange;
NSRange resultRange;
searchRange.location = 0;
searchRange.length = number.length;
while (0 < searchRange.length)
{
resultRange = [number rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]
options:NSLiteralSearch
range:searchRange];
tmpString = [number substringWithRange:resultRange];
normalizedNumber = [normalizedNumber stringByAppendingString:tmpString];
searchRange.location = resultRange.location + resultRange.length;
searchRange.length = number.length - searchRange.location;
}
return normalizedNumber;
}
[NSString rangeOfCharacterFromSet options range] example
// cut a string by words
- (NSString *)stringCutByWordsToMaxLength:(int)length
{
NSCharacterSet *whitespaceCharacterSet =
[NSCharacterSet whitespaceCharacterSet];
// to consider: a range check on length here?
NSRange relevantRange = NSMakeRange(0, length);
// find beginning of last word
NSRange lastWordRange =
[self rangeOfCharacterFromSet:whitespaceCharacterSet
options:NSBackwardsSearch
range:relevantRange];
// if the last word was the first word of the string,
// consume the whole string; this looks to be the same
// effect as the original scan forward given that the
// assumption is already made in the scan backwards that
// the string doesn't end on a whitespace; if I'm wrong
// then get [whitespaceCharacterSet invertedSet] and do
// a search forwards
if(lastWordRange.location == NSNotFound)
{
lastWordRange = relevantRange;
}
// adjust the range to include dependent chars
stringRange = [self rangeOfComposedCharacterSequencesForRange:stringRange];
// Now you can create the short string
NSString *string = [self substringWithRange:stringRange];
return [NSString stringWithFormat:@"%@...",string];
}