我正在尝试删除iOS中字符串中的所有括号。看到这个stackoverflow帖子:how to remove spaces, brackets and " from nsarray
答案:
NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"()"];
s = [s stringByTrimmingCharactersInSet:charsToTrim];
但这只是删除一个括号(来自mystring。如何删除所有这些?
我的代码如下所示:
NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"( )"];
attributedLabel.text = [attributedLabel.text stringByTrimmingCharactersInSet:charsToTrim];
NSLog(@"%@",attributedLabel.text);
需要一些指导...
答案 0 :(得分:15)
尝试使用
[[attributedLabel.text stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];
代替。
答案 1 :(得分:2)
以下代码正在运作
NSString *str=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"string"];
NSLog(@"%@",[[str stringByReplacingOccurrencesOfString:@")" withString:@""] stringByReplacingOccurrencesOfString:@"(" withString:@""]);
答案 2 :(得分:2)
只需按照以下方式执行此操作:
NSString *finalStr = [initialStr stringByReplacingOccurenceOfString:@"(" withString:@""];
finalStr = [finalStr stringByReplacingOccurenceOfString:@")" withString:@""];
这将从字符串中删除所有括号:我在此开发人员blog上找到了这个答案
答案 3 :(得分:0)
Swift 4.2使用replacingOccurrences
例如从(Pizza)获取披萨
let foodWithParentheses = "(Pizza)"
let pizzaWithoutParen = foodWithParentheses.replacingOccurrences(of: "(", with: "").replacingOccurrences(of: ")", with: "")
print(pizzaWithoutParen)
// output: Pizza