I can't seem to find the answer to this anywhere. I can do it in c but objective c is difficult.
I want to cut the end of a string after a certain character
so user@example.com will become user (cut at '@')
How do I do this?
I can't seem to find the answer to this anywhere. I can do it in c but objective c is difficult.
I want to cut the end of a string after a certain character
so user@example.com will become user (cut at '@')
How do I do this?
This will give you the first chunk of text that comes before your special character.
NSString *separatorString = @"@";
NSString *myString = @"user@example.com";
NSString *myNewString = [myString componentsSeparatedByString:separatorString].firstObject;
You can use a combination of substringToIndex:
and rangeOfString:
methods, like this:
NSString *str = @"user@example.com";
NSRange pos = [str rangeOfString:@"@"];
if (pos.location != NSNotFound) {
NSString *prefix = [str substringToIndex:pos.location];
}
Notes:
NSNotFound
to ensure that the position is valid.substringToIndex:
excludes the index itself, so the @
character would not be included.