Home>Article>Backend Development> PHP function strncmp() for string comparison of the first n characters (case sensitive)
Example
Compare twostrings(case sensitive):
Definition and usage
strncmp()FunctionCompare two strings (case sensitive).
Note: strncmp() is binarysafeand case-sensitive.
Tips: This function is similar to the strcmp() function, except that strcmp() does not have a length parameter.
Syntax
strncmp(string1,string2,length)
Parameters | Description |
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to be compared. |
length | Required. Specifies the number of characters per string used for comparison. |
Technical details
Return value: | This function returns:
|
4+ |
Example 1
Compare two strings (case-sensitive, Hello and hELLo output are different):
"; echo strncmp("Hello","hELLo",6); ?>
Example This example uses the strncmp function to compare the specified length String.
int main() { char str1[]="Hello",str2[]="Help",str3[]="Hello"; int a,b,c; a = strncmp(str1,str2,3); //比较字符串str1,str2前3个字符 b = strncmp(str2,str3,4); //比较字符串str2,str3前4个字符 c = strncmp(str1,str2,4); //比较字符串str1,str2前4个字符 cout<Run result:
0 1 -1The above is the detailed content of PHP function strncmp() for string comparison of the first n characters (case sensitive). For more information, please follow other related articles on the PHP Chinese website!