Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

objective c - Removing parentheses from the string in iOS

I am trying to remove all the parentheses in my string in iOS. Saw this stackoverflow post: how to remove spaces, brackets and " from nsarray

The answer:

NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"()"];
s = [s stringByTrimmingCharactersInSet:charsToTrim];

But this just removes one bracket which is ( from mystring. How to remove all of them?

My code looks like this:

NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"( )"];
attributedLabel.text = [attributedLabel.text stringByTrimmingCharactersInSet:charsToTrim];
NSLog(@"%@",attributedLabel.text);

Need some guidance on this...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using
[[attributedLabel.text stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];
instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...