开发者

How to compare a mathing string location?

开发者 https://www.devze.com 2023-04-10 04:32 出处:网络
NSString *str0 = @\"stackoverflow\"; NSString *str1 = @\"stackovarflow\"* NSString *str2 = @\"stackoverflowing\"*

NSString *str0 = @"stackoverflow";

NSString *str1 = @"stackovarflow"*

NSString *str2 = @"stackoverflowing"*

I want str0 开发者_开发知识库and str1 and so on compared equal to location.

str0 isEqualToLocation:str1 result is 5(start Index 0) or 6(start Index 1)

str1 isEqualToLocation:str2 result is 12(start Index 0) or 13(start Index 1)

if not equalToString return is -1

how to know matching a string location?


using a for loop (note this is psuedocode not objective-c) I assume this is what you want. If so shouldn't str0 str1 return 6 not 5?

int compare(char* str0, char* str1) {
    //as per your last request although i'd return 0 if it were me
    if (str0[0] != str1[0]) { return -1 }
    for (int i = 0; i < Min(len(str1), len(str2)) {
        if (str0[i] != str1[i]) {
            return i-1;
        }
    }
    return Min(len(str1), len(str2))
}
0

精彩评论

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