检查nsstring是否匹配另一个字符串 [英] Checking if a nsstring matches another string

查看:106
本文介绍了检查nsstring是否匹配另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串167282。如何检查字符串是否包含128?有任何规定知道匹配第一个字符串的第二个字符串的百分比吗? (如果第二个字符串中的所有字符都存在于第一个字符串中或不存在)。

Suppose I have a string "167282". How can I check if the string contains "128"? Is there any provision to know the percentage of the 2nd string that matches the first string? (If all the characters of the 2nd string are present in the first string or not.) Please help and thanx in advance.

推荐答案

使用以下命令:

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中匹配的总字符数的百分比。这是,正如我所能告诉的,你打算与你的问题 - 百分比匹配的概念不会有任何意义,否则。

Where matchingCount will be the number of characters in compareString that match a character in @"167282" and percentMatching will be the percent of total characters in compareString that match. This is, as best as I can tell, what you intended with your question - the concept of a percent match wouldn't make any sense otherwise.

这篇关于检查nsstring是否匹配另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆