如何有选择地修剪NSMutableString? [英] How to selectively trim an NSMutableString?
问题描述
我想知道如何选择性地修剪NSMutableString.例如,如果我的字符串是"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist",我该如何以编程方式剪裁除单词"MobileSafari"以外的所有内容?
I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?
注意:鉴于上述以编程方式,我希望该解决方案仍然有效,即使将单词"MobileSafari"更改为"Youtube",或该单词"Jared's-iPhone"更改为"Angela's-iPhone".
Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".
非常感谢您的帮助!
推荐答案
测试代码:100%可行
NSString *inputString=@"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *array= [inputString componentsSeparatedByString:@"_"];
if ([array count]>0) {
NSString *resultedString=[array objectAtIndex:0];
NSLog(@" resultedString IS - %@",resultedString);
}
输出:
resultedString IS - MobileSafari
这篇关于如何有选择地修剪NSMutableString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!