views:

50

answers:

2

I have a NSString like this:

456673\tSomething

But I would like to extract Something only.... ... All the data must be in this format ....

xxxx\tyyyy How can I split it bases on \t? thank you.

+3  A: 

You may be looking for this instance method:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

If that fails your needs and you want something more powerful, I can personally recommend RegexKitLite. RegexKitLite adds the power of regular expressions to NSString in the form of a category.

Greg Sexton
+1  A: 

I've added a category to NSString for convenience (class: NSString+Utility):

- (NSString *)substringFromFirstOccurenceOfString:(NSString *)string {
    NSRange range = [self rangeOfString:string];

    if (range.location != NSIntegerMax) {
        int index = range.location + range.length;
        return [self substringFromIndex:index];
    } else {
        return self;
    }
}

- (NSString *)substringFromLastOccurenceOfString:(NSString *)string {
    NSRange range = [self rangeOfString:string options:NSBackwardsSearch];

    if (range.location != NSIntegerMax) {
        int index = range.location + range.length;
        return [self substringFromIndex:index];
    } else {
        return self;
    }
}

- (NSString *)substringToFirstOccurenceOfString:(NSString *)string {
    NSRange range = [self rangeOfString:string];    

    if (range.location != NSIntegerMax) {
        int index = range.location + range.length;
        return [self substringToIndex:index];            
    } else {
        return self;
    }
}

- (NSString *)substringToLastOccurenceOfString:(NSString *)string {
    NSRange range = [self rangeOfString:string options:NSBackwardsSearch];  

    if (range.location != NSIntegerMax) {
        int index = range.location;
        return [self substringToIndex:index];       
    } else {
        return self;
    }
}
Wolfgang Schreurs