strncmp("sample","sam",4) returns 1 because the final requirement is if one string terminates before len, then the other must also terminate at that position.
You can imagine that all your strings have one more final, invisible "termination" character. If that termination character happens to be within in len, then it must match, too.
For instance, write that termination character with, say, the sequence "\0". Then you can equivalently consider that function call as strncmp("sample\0","sam\0",4).
So, the "p" in "sample" does not match the termination character in "sam".
strncmp
説明
int strncmp ( string str1, string str2, int len )この関数は、strcmp()に似てますが、 各文字列から(最大)文字数(len)を比較に使用 するところが異なります。文字列長がlenより も短い場合、その文字列の長さが比較に使用されます。
str1がstr2より小さ いに場合?< 0、str1が str2より大きい場合に > 0 、等しい場合に 0 を返します。
比較は大文字小文字を区別することに注意してください。
preg_match()、 ereg()、 strcasecmp()、substr()、 stristr()、strcmp() および strstr() も参照ください。
strncmp
Anonymous
17-Apr-2002 08:46
17-Apr-2002 08:46