How do you check if an NSString begins with a certain character (the character *).
The * is an indicator for the type of the cell, so I need the contents of this NSString without the *, but need to know if the * exists.
How do you check if an NSString begins with a certain character (the character *).
The * is an indicator for the type of the cell, so I need the contents of this NSString without the *, but need to know if the * exists.
Use characterAtIndex:
. If the first character is an asterisk, use substringFromIndex:
to get the string sans '*'.
You can use:
NSString *newString;
if ( [myString characterAtIndex:0] == '*' ) {
newString = [myString substringFromIndex:1];
}
This might help? :)
Just search for the character at index 0 and compare it against the value you're looking for!
NSString *stringWithoutAsterisk(NSString *string) {
NSRange asterisk = [string rangeOfString:@"*"];
return asterisk.location == 0 ? [string substringFromIndex:1] : string;
}
You can use the -hasPrefix:
method of NSString
:
NSString* output = nil;
if([string hasPrefix:@"*"])
output = [string substringFromIndex:1];