Python has string.startswith()
and string.endswith()
functions which are pretty useful.
What NSString methods can I use to have the same function?
The endswith() method returns True if a string ends with the specified suffix. If not, it returns False .
The endsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
The endswith() function returns True if a string ends with the specified suffix (case-sensitive), otherwise returns False. A tuple of string elements can also be passed to check for multiple options. If a string ends with any element of the tuple then the endswith() function returns True.
Use -hasPrefix:
and -hasSuffix:
:
NSString *s = @"foobar";
NSLog(@"%d %d\n", [s hasPrefix:@"foo"], [s hasSuffix:@"bar"]);
// Output: "1 1"
You want the hasPrefix
and hasSuffix
messages.
I tend to also use the compare:options:
message pretty regularly to achieve the same but with case-insensitive comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With