I have to match the number of occurrences of n special characters in a string.
I thought to create an array with all these chars (they are 20+) and create a function to match each of them.
I just have the total amount of special characters in the string, so I can make some math count on them.
我必须匹配字符串中n个特殊字符的出现次数。我想创建一个包含所有这些字符的数组(它们是20+)并创建一个匹配它们的函数。我只是在字符串中有特殊字符的总数,所以我可以对它们进行一些数学计算。
So in the example:
所以在这个例子中:
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
NSArray *myArray = [NSArray arrayWithObjects:@"#",@"@",@"&",nil];
The function should return 5.
该函数应返回5。
Would it be easier match the characters that are not in the array, take the string length and output the difference between the original string and the one without special chars?
Is this the best solution?
是否更容易匹配不在数组中的字符,获取字符串长度并输出原始字符串和没有特殊字符的字符串之间的差异?这是最好的解决方案吗?
3 个解决方案
#1
10
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
//even in first continuous special letters it contains -it will return 8
//NSString *myString = @"#&#My string #full# of speci@l ch@rs & symbols";
NSArray *arr=[myString componentsSeparatedByCharactersInSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"#@&"]];
NSLog(@"resulted string : %@ \n\n",arr);
NSLog(@"count of special characters : %i \n\n",[arr count]-1);
OUTPUT:
resulted string : (
结果字符串:(
"My string ",
full,
" of speci",
"l ch",
"rs ",
" symbols"
)
count of special characters : 5
#2
2
You should utilize an NSRegularExpression, its perfect for your scenario. You can create one like this:
您应该使用NSRegularExpression,它非常适合您的场景。您可以创建一个这样的:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|&)" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
Caveat: I ripped the code from the Apple Developer site. And I'm no regex guru so you will have to tweak the pattern. But you get the gist.
警告:我从Apple开发者网站上删除了代码。而且我不是正则表达式大师所以你必须调整模式。但你得到了要点。
#3
1
You should look also at NSRegularExpression
:
您还应该查看NSRegularExpression:
- (NSUInteger)numberOfCharacters:(NSArray *)arr inString:(NSString *)str {
NSMutableString *mutStr = @"(";
for(i = 0; i < [arr count]; i++) {
[mutStr appendString:[arr objectAtIndex:i]];
if(i+1 < [arr count]) [mutStr appendString:@"|"];
}
[mutStr appendString:@")"];
NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:mutStr options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger *occur = [regExnumberOfMatchesInString:str options:0 range:NSMakeRange(0, [string length])];
[mutStr release];
return occur;
}
Usage example:
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
NSArray *myArray = [NSArray arrayWithObjects:@"#",@"@",@"&",nil];
NSLog(@"%d",[self numberOfCharacters:myArray inString:myString]); // will print 5
#1
10
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
//even in first continuous special letters it contains -it will return 8
//NSString *myString = @"#&#My string #full# of speci@l ch@rs & symbols";
NSArray *arr=[myString componentsSeparatedByCharactersInSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"#@&"]];
NSLog(@"resulted string : %@ \n\n",arr);
NSLog(@"count of special characters : %i \n\n",[arr count]-1);
OUTPUT:
resulted string : (
结果字符串:(
"My string ",
full,
" of speci",
"l ch",
"rs ",
" symbols"
)
count of special characters : 5
#2
2
You should utilize an NSRegularExpression, its perfect for your scenario. You can create one like this:
您应该使用NSRegularExpression,它非常适合您的场景。您可以创建一个这样的:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(#|&)" options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
Caveat: I ripped the code from the Apple Developer site. And I'm no regex guru so you will have to tweak the pattern. But you get the gist.
警告:我从Apple开发者网站上删除了代码。而且我不是正则表达式大师所以你必须调整模式。但你得到了要点。
#3
1
You should look also at NSRegularExpression
:
您还应该查看NSRegularExpression:
- (NSUInteger)numberOfCharacters:(NSArray *)arr inString:(NSString *)str {
NSMutableString *mutStr = @"(";
for(i = 0; i < [arr count]; i++) {
[mutStr appendString:[arr objectAtIndex:i]];
if(i+1 < [arr count]) [mutStr appendString:@"|"];
}
[mutStr appendString:@")"];
NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:mutStr options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger *occur = [regExnumberOfMatchesInString:str options:0 range:NSMakeRange(0, [string length])];
[mutStr release];
return occur;
}
Usage example:
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
NSArray *myArray = [NSArray arrayWithObjects:@"#",@"@",@"&",nil];
NSLog(@"%d",[self numberOfCharacters:myArray inString:myString]); // will print 5