Instance
Comparison of two strings (case-sensitive):
<?phpecho strncmp ("Hello world!", "Hello earth!", 6);? >
Definition and usage
The strncmp () function compares two strings (case sensitive).
Note: strncmp () is binary safe and case-sensitive.
Tip: This function is similar to the strcmp () function, unlike the strcmp () without the length parameter.
Grammar
STRNCMP (String1,string2,length)
parameters |
description |
string1 |
required. Specifies the first string to compare. |
string2 |
required. Specifies a second string to compare. |
length |
required. Specifies the number of characters each string is used to compare. |
technical details
return value: |
This function returns:
-
0-if two strings are equal
-
<0-if string1 is less than string2
-
>0-if string1 is greater than string2
|
php version: |
4+ |
More examples
Example 1
Compare two strings (case-sensitive, hello and hello output are not the same):
<?phpecho strncmp ("Hello", "Hello", 6), echo "<br>", Echo strncmp ("Hello", "Hello", 6);? >
Example This example uses the STRNCMP function to compare strings of a specified length.
int main () { char str1[]= "Hello", str2[]= "Help", str3[]= "Hello"; int a,b,c; A = strncmp (str1,str2,3); Compare string Str1,str2 first 3 characters B = strncmp (str2,str3,4); Compare string Str2,str3 first 4 characters C = strncmp (str1,str2,4); Compare string Str1,str2 first 4 characters cout<<a<< "\ t" <<b<< "\ T" <<c<< "\ n";}
Operation Result:
0 1-1