截断从特定字符开始的NSString的一部分 [英] Truncate part of an NSString starting at a particular character
本文介绍了截断从特定字符开始的NSString的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串
NSString * myOldString = @"This is a string (and this part is between brackets)"
现在,我想以这样的方式截断字符串,括号,被截断。
Now, I want to truncate the string in such a way, that basically everything between the brackets, including the brackets, is truncated.
更确切地说:我不在乎第一个括号之后发生了什么。
More precisely: I don't care what is happening after the first bracket at all.
我不能做一个简单的 stringByReplacingOccurrencesOfString:
,因为我不能预测括号之间将是什么。所以,结果字符串应该是:
I can't do a simple stringByReplacingOccurrencesOfString:
, because I cannot predict what will be between the brackets. So, the resulting string should be:
"This is a string"
推荐答案
一种方法:
NSArray *components = [myOldString componentsSeparatedByString:@"("];
NSString *newString = [components objectAtIndex:0];
应该也适用于没有'('
Should also work for the case where there isn't a '('
这篇关于截断从特定字符开始的NSString的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文