gpt4 book ai didi

iphone - 匹配字符 Objective-C 的 NSArray

转载 作者:太空狗 更新时间:2023-10-30 03:31:45 24 4
gpt4 key购买 nike

我必须匹配字符串中 n 个特殊字符的出现次数。
我想创建一个包含所有这些字符的数组(它们超过 20 个)并创建一个函数来匹配它们中的每一个。
我只有字符串中特殊字符的总数,所以我可以对它们进行一些数学计算。

所以在这个例子中:

NSString *myString = @"My string #full# of speci@l ch@rs & symbols";
NSArray *myArray = [NSArray arrayWithObjects:@"#",@"@",@"&",nil];

函数应该返回 5。

匹配不在数组中的字符,取字符串长度并输出原始字符串和没有特殊字符的字符串之间的差异会更容易吗?
这是最好的解决方案吗?

最佳答案

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

输出:

结果字符串:(

"My string ",

full,

" of speci",

"l ch",

"rs ",

" symbols"

)

count of special characters : 5 

关于iphone - 匹配字符 Objective-C 的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6876036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com