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

时间:2021-04-18 20:17:36

I want to trim the occurence of special characters in a string

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

String  has :    
prevs: Case Number                                                                    
____________________

In this i want to remove -------- this dash from this string .I have tried like this :

在这里我想从这个字符串中删除--------这个破折号。我试过这样:

NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"-"];
    NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:@""];

Thanks in Advance!

提前致谢!

4 个解决方案

#1


1  

Try This:

尝试这个:

NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@""];

#2


0  

NSString *trimedString = [myString stringByReplacingOccurrencesOfString:@"-" withString:@""];

#3


0  

Use regular expression, the pattern [\\s_]{4,} searches for 4 and more whitespace or underscore characters.

使用正则表达式,模式[\\ s _] {4,}搜索4个或更多的空格或下划线字符。

NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];

If the underscore characters are really dashes reaplace the character in the pattern.

如果下划线字符真的是破折号,则重新绘制模式中的字符。

#4


0  

You can use below code to remove your special string.

您可以使用以下代码删除您的特殊字符串。

NSString *yourString = @"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""];
NSLog (@"Your final string : %@", finalString);

#1


1  

Try This:

尝试这个:

NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:@"-" withString:@""];

#2


0  

NSString *trimedString = [myString stringByReplacingOccurrencesOfString:@"-" withString:@""];

#3


0  

Use regular expression, the pattern [\\s_]{4,} searches for 4 and more whitespace or underscore characters.

使用正则表达式,模式[\\ s _] {4,}搜索4个或更多的空格或下划线字符。

NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:@"[\\s_]{4,}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];

If the underscore characters are really dashes reaplace the character in the pattern.

如果下划线字符真的是破折号,则重新绘制模式中的字符。

#4


0  

You can use below code to remove your special string.

您可以使用以下代码删除您的特殊字符串。

NSString *yourString = @"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:@""];
NSLog (@"Your final string : %@", finalString);