Objective-C (cocoa) equivalent to python's endswith/beginswith

后端 未结 3 1694
遇见更好的自我
遇见更好的自我 2021-01-17 09:37

Python has string.startswith() and string.endswith() functions which are pretty useful. What NSString methods can I use to have the same function?<

相关标签:
3条回答
  • 2021-01-17 10:05

    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.

    0 讨论(0)
  • 2021-01-17 10:10

    Use -hasPrefix: and -hasSuffix::

    NSString *s = @"foobar";
    NSLog(@"%d %d\n", [s hasPrefix:@"foo"], [s hasSuffix:@"bar"]);
    // Output: "1 1"
    
    0 讨论(0)
  • 2021-01-17 10:11

    -hasPrefix() and -hasSuffix() return YES or NO depending on whether the receiver begins or ends with the given substring. If that's what startswith() and endswith() do, then that's your answer.

    0 讨论(0)
提交回复
热议问题