How to compare a mathing string location?
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))
}
精彩评论