ios - 按特定单词将 NSString 拆分为数组

标签 ios objective-c arrays nsstring

我需要按特定单词将 NSString 拆分为数组。

我尝试使用[componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@""]]

但是分割是由单个字符执行的,我需要几个字符。

例子:

NSString @"Hello great world";

分割键==@“很棒”;

结果:

array[0] == @"Hello";
array[1] == @"world";

最佳答案

尝试

NSString *str = @"Hello great world"; 
//you can use the bellow line to remove space   
//str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];       
// split key = @"great"

NSArray *arr = [str componentsSeparatedByString:@"great"];

关于ios - 按特定单词将 NSString 拆分为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208899/

相关文章:

jquery - 按回车键隐藏 iPad 键盘

ios - 如果用户回答传入的 iMessage,WKWebView 会崩溃

ios - 使用 Set 时,Swift 2.0 : Could not cast value MyApp. MyCustomClass 到 MyAppTests.MyCustomClass

ios - 如何创建具有不同纹理的 SCNNode 的新实例

ios - 每次重新加载表格时,自定义 UITableViewCell 中的 UILabel 都会向上移动

ios - 使用 UIActivityViewController 在 Facebook 的同一帖子上分享多张照片

objective-c - 定义方法的属性时+和-有什么区别?

PHP,将多维数组中的所有数组递归提取到一个平面 "holder"数组?

arrays - 在 Rust 中如何在不同大小的数组之间进行复制?

python - A[0] 和 A[0 :1] numpy arrays in python 之间的区别