开发者

How can get sequence match count in NSString objects by using NSCharacterset class?

Always i have a mind thanks for your help..

I'm trying to count sequence matchs NSString objects by using NSCharacterset class.

It seems difficult question...

NSString *StringFirst = @"ABCDEFGHI";  <= Sequence characters.
NSString *StringSecond = @"DGHIJ";

// StringSecond object compare sequence in StringFirst object.
// result is following.

result : 2   (Because StringSec开发者_开发知识库ond object D -> G sequence no matching in StringFirst,
              G -> H matches,    so count : 1
              H -> I matches,    so count : 2
              I -> J no matches  so count : 2)

Gentleman, Please help..


I don't know why you want to accomplish this using NSCharacterset class and I presume there is no way to do that since NSCharacterset handles individual characters, since in your case a string is to be checked. Here is an alternative, hope it may help.

NSString *StringFirst = @"ABCDEFGHI";
NSString *StringSecond = @"DGHIJ";
int count = 0;
for (int i=0; i< [StringSecond length]; i++) {

        if (i+2 <= [StringSecond length]) {
            NSString *subStringFromSecond = [StringSecond substringWithRange:NSMakeRange(i, 2)];
            NSRange range = [StringFirst rangeOfString:subStringFromSecond];
            if (range.length) {
                count++;
            }
        }

    }
// count gives matched count, 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜