1、用这个方法去掉所有的空格;
- (NSString *)inplaceOfAnswerByStanderAnswer:(NSString *)standerAnswer {
// NSString *theString = @" Hello this is a (long) string! ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *parts = [standerAnswer componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
standerAnswer = [filteredArray componentsJoinedByString:@""];
return [self removeKuoHaoBy:standerAnswer];
}
2、用这个方法移除括号内的内容;
- (NSString *)removeKuoHaoBy:(NSString *)originalString {
NSMutableString * muStr = [NSMutableString stringWithString:originalString];
while (1) {
NSRange range = [muStr rangeOfString:@"("];
NSRange range1 = [muStr rangeOfString:@")"];
if (range.location != NSNotFound) {
NSInteger loc = range.location;
NSInteger len = range1.location - range.location;
[muStr deleteCharactersInRange:NSMakeRange(loc, len + 1)];
}else{
break;
}
}
return muStr;
}