NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(0, end.location)]];
If you wanted grab a string between two characters, you could do:
NSRange start = [_contentsOfElement rangeOfString:@"|"];
NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(start.location, end.location)]];
very nice tip!! thanks!!
ReplyDeleteThanks! But I'm not sure the end.location is handled correctly... because the substringWithRange shoud expect the starting position and the length.
ReplyDeleteYeh should be using int _length = end.location - start.location; and then using that as the 2nd param in substringWithRange.
ReplyDelete