iphone - 根据不区分大小写的子字符串拆分字符串

标签 iphone ios xcode ipad

我需要根据单词拆分字符串。该词可能属于任何大小写,我需要保留子字符串数组的大小写。示例:

  1. 字符串可能是
    a) “我叫 XYZ,住在 ABC。”或者
    b) “我叫 XYZ,住在 ABC。”或者
    c) “我叫 XYZ,住在 ABC。”
  2. 现在,分隔字符串可以是“and”或“AND”或“And”。
  3. 在我的代码中,我不知道 a)、b) 和 c) 中使用了哪个字符串。
  4. 问题是如何将字符串分隔为“My name is XYZ”和“I live in ABC”。分别。

最佳答案

你可以使用

NSRange range = [string rangeOfString:@" and " options: NSCaseInsensitiveSearch];

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

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

关于iphone - 根据不区分大小写的子字符串拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520814/

相关文章:

iphone - 禁用核心图 iPhone 中的滚动

ios - UINavigationController 跳过推送当前 View Controller

swift - 如何更改 UITableView 部分边框?

swift - Swift 2.2 上的可选绑定(bind)错误?

ios - Xcode 9.2 模拟器调试坏了;无法附加到进程 ID

iphone - 如何更改 UIBarButtonItem 标题 - iPhone SDK

ios - 如何使用 swift 更改 iPhone 移动 iOS 中的下载路径

iphone - 通过 IAP 每月订阅

Ios - 识别二维码响应类型

ios - UIImagePickerController 在打开相机时崩溃