Sunday, May 19, 2013

NSString rangeOfCharacterFromSet options example ios

[NSString rangeOfCharacterFromSet options]

Finds and returns the range in the receiver of the first character, using given options, from a given character set.
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask
A character set. This value must not be nil.

Important: Raises an NSInvalidArgumentException if aSet is nil.
A mask specifying search options. The following options may be specified by combining them with the C bitwise OR operator: NSCaseInsensitiveSearch,NSLiteralSearchNSBackwardsSearch. See String Programming Guide for details on these options.
Return Value of [NSString rangeOfCharacterFromSet options]
The range in the receiver of the first character found from aSet. Returns a range of{NSNotFound, 0} if none of the characters in aSet are found.
Discussion of [NSString rangeOfCharacterFromSet options]
Invokes rangeOfCharacterFromSet:options:range: with mask for the options and the entire extent of the receiver for the range.
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] example
NSString* str = @"hdskfh   dsakjfh akhf kasdhfk asdfkjash fkadshf1234        ";
NSRange rng = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: [str stringByReplacingOccurrencesOfString: @" " withString: @""]] options: NSBackwardsSearch];
str = [str substringToIndex: rng.location+1];
[NSString rangeOfCharacterFromSet options] example
- (BOOL)my_hasWhitespaceOrNewlineSuffix {
  NSRange r = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
  BOOL foundChar = r.location != NSNotFound;
  BOOL isLineFinal = foundChar && (r.location + r.length == [self length]);
  return isLineFinal;
[NSString rangeOfCharacterFromSet options] example
@interface NSString (MDBase64Additions)
- (BOOL)isBase64Data;

@implementation NSString (MDBase64Additions)

- (BOOL)isBase64Data {
    if ([self length] % 4 == 0) {
        static NSCharacterSet *invertedBase64CharacterSet = nil;
        if (invertedBase64CharacterSet == nil) {
            invertedBase64CharacterSet = [[[NSCharacterSet
                                     invertedSet] retain];
        return [self rangeOfCharacterFromSet:invertedBase64CharacterSet
           options:NSLiteralSearch].location == NSNotFound;
    return NO;