Objective c 如何在Objective c中修剪字符串中的特殊字符
我想修剪字符串中出现的特殊字符 String has : prevs: Case Number ____________________ 提前谢谢 试试这个:Objective c 如何在Objective c中修剪字符串中的特殊字符,objective-c,nsstring,trim,Objective C,Nsstring,Trim,我想修剪字符串中出现的特殊字符 String has : prevs: Case Number ____________________ 提前谢谢 试试这个: NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@"
NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@""];
使用正则表达式,模式
[\\s\uz]{4,}
将搜索4个或更多的空格或下划线字符
NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];
如果下划线字符真的是破折号,请在模式中重新放置该字符。您可以使用下面的代码删除特殊字符串
NSString *yourString = @"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""];
NSLog (@"Your final string : %@", finalString);
@谢谢,它起作用了
NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];
NSString *yourString = @"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""];
NSLog (@"Your final string : %@", finalString);