基于不区分大小写的子串拆分字符串

Jam*_*obo 0 iphone xcode ipad ios

我需要根据单词拆分字符串.该单词可以是任何Case,我需要保留子字符串数组的大小写.例:

  1. 字符串可能是
    a)"我的名字是XYZ,我住在ABC." 或
    b)"我的名字是XYZ,我住在ABC." 或者
    c)"我的名字是XYZ,我住在ABC."
  2. 现在,分离串可以是"和"或"和"或"和".
  3. 在我的代码中,我不知道在a),b)和c)中使用了哪个字符串.
  4. 问题是如何将字符串分隔为"我的名字是XYZ"和"我住在ABC".分别.

Mar*_*n R 6

您可以使用

NSRange range = [string rangeOfString:@" and " options: NSCaseInsensitiveSearch];
Run Code Online (Sandbox Code Playgroud)

以不区分大小写的方式查找分隔字符串的位置,然后使用提取第一个和最后一个部分

if (range.location != NSNotFound) {
    NSString *first = [string substringToIndex:range.location];
    NSString *last = [string substringFromIndex:(range.location + range.length)];
} else {
    // separator not found ...
}
Run Code Online (Sandbox Code Playgroud)