Sunday, January 8, 2012

extract the substring from NSString

Extract the substring from/to certain position:



[myString substringToIndex:index];
[myString substringFromIndex:index];

Extract the substring from beginning to the particular position:

NSRange end = [longString rangeOfString:@";"];
NSString *shortString =[longString substringWithRange:NSMakeRange(0, end.location)]];
NSLog("%@", shortString);

Extract a string between two characters:

NSRange start = [longString rangeOfString:@"("];
NSRange end = [longString rangeOfString:@")"];
NSString *shortString = [longString substringWithRange:NSMakeRange(start.location, end.location)]];
NSLog("%@", shortString);

No comments:

Post a Comment