假设我有一个字符串“167282”。如何检查字符串是否包含“128”?是否有任何规定可以知道第二个字符串与第一个字符串匹配的百分比? (如果第二个字符串的所有字符都出现在第一个字符串中。)请提前帮忙并感谢。
使用以下内容:
NSString *compareString = @"128";
NSCharacterSet *compareCharSet = [NSCharacterSet characterSetWithCharactersInString:@"167282"];
NSUInteger strLen = [compareString length];
NSUInteger matchingCount = 0;
for (NSUInteger i = 0; i < strLen; ++i) {
if ([compareCharSet characterIsMember:[compareString characterAtIndex:i]])
++matchingCount;
}
float percentMatching = matchingCount/(float)strLen;
其中matchingCount 是compareString 中与@"167282" 中的字符匹配的字符数,percentMatching 是compareString 中匹配的字符总数的百分比。据我所知,这就是您的问题的意图 - 否则百分比匹配的概念没有任何意义。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)