PHP in the first n character string comparison (case sensitive) function strncmp ()

Source: Internet
Author: User
Tags strcmp

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.