Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在Objective c中修剪字符串中的特殊字符_Objective C_Nsstring_Trim - Fatal编程技术网

Objective c 如何在Objective c中修剪字符串中的特殊字符

Objective c 如何在Objective c中修剪字符串中的特殊字符,objective-c,nsstring,trim,Objective C,Nsstring,Trim,我想修剪字符串中出现的特殊字符 String has : prevs: Case Number ____________________ 提前谢谢 试试这个: NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@"

我想修剪字符串中出现的特殊字符

String has : prevs: Case Number ____________________ 提前谢谢

试试这个:

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);