pas*_*ine 8 iphone character objective-c nsarray nsrange
我必须匹配字符串中n个特殊字符的出现次数.
我想创建一个包含所有这些字符的数组(它们是20+)并创建一个匹配它们的函数.
我只是在字符串中有特殊字符的总数,所以我可以对它们进行一些数学计算.
所以在这个例子中:
NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
NSArray *myArray = [NSArray arrayWithObjects:@"#",@"@",@"&",nil];
Run Code Online (Sandbox Code Playgroud)
该函数应返回5.
是否更容易匹配不在数组中的字符,获取字符串长度并输出原始字符串和没有特殊字符的字符串之间的差异?
这是最好的解决方案吗?
Vij*_*com 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);
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
结果字符串:(
"My string ",
full,
" of speci",
"l ch",
"rs ",
" symbols"
Run Code Online (Sandbox Code Playgroud)
)
count of special characters : 5
Run Code Online (Sandbox Code Playgroud)