Differences between PHPstrcmp, strnatcmp, and strncmp functions, strnatcmpstrncmp_PHP tutorial

Source: Internet
Author: User
Differences between PHPstrcmp, strnatcmp, and strncmp functions: strnatcmpstrncmp. Differences between PHPstrcmp, strnatcmp, and strncmp functions. strnatcmpstrncmp string comparison indicates that when s1s2 is returned, a negative number is returned. when s1s2 is returned, 0 is returned. when s1s2 is returned, a positive number is returned. 1? Php2 $ st PHP strcmp, strnatcmp, strncmp functions, strnatcmpstrncmp

String comparison
When s1 When s1 = s2, the return value is 0.
When s1> s2, a positive number is returned.

1
 "; 5 // output-1 6 echo strnatcmp ($ str1, $ str2 )."
"; 7 // output 1 8 echo strncmp ($ str1, $ str2, 5 )."
"; 9 // output-110 11?>

The strcmp (str1, str2) function compares two strings by character, that is, two strings are compared from left to right one by character (compared by ASCII value ), until different characters or '\ 0' are displayed. For example: "A" <"B" a ">" A "" computer ">" compare"

Strnatcmp (str1, str2) functions use the natural sorting algorithm to compare strings. this function provides a comparison algorithm that is used by humans to sort numeric strings. this is the "natural order ".

For the comparison principle of strncmp (str1, str2, len) functions, refer to strcmp (str1, str2) functions. The difference is that only len characters starting with a string are compared.

Ps: The above three functions are case-sensitive. if the comparison strings are case-insensitive, there are three other similar functions: strcasecmp, strnatcasecmp, and strncasecmp.


String comparison functions strcmp () and strncmp ()

Comparison:
Function prototype: extern int strcmp (const char * s1, const char * s2 );
Int strncmp (char * str1, char * str2, int maxlen );
It can be seen that strncmp has one more parameter maxlen, and the difference is here, which indicates comparing the character string before maxlen length

The two return values are also different:
Strcmp, when s1 When s1 = s2, 0 is returned.
When s1> s2, a positive number is returned.
Strncmp: If the first maxlen bytes are completely equal, the return value is 0;
If the difference between str1 [n] and str2 [n] occurs during the prior maxlen byte comparison, return (str1 [n]-str2 [n]).

String comparison functions strcmp () and strncmp ()

Function: int strcmp (const char * s1, const char * s2)
This function is used to compare s1 and s2 strings. This function returns a value whose symbols are related to the comparison results of the first pair of different characters.
If the two strings are equal, strcmp returns 0.
If s1 is a substring of s2, s1 is smaller than s2
There are also functions
Int strncmp (const char * s1, const char * s2, size_t size)
This function is very similar to strcmp. The difference is that the strncmp function specifies to compare the size of characters. That is to say, if the first size character of string s1 is the same as that of string s2, the return value of the function is 0.
 

Http://www.bkjia.com/PHPjc/887684.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/887684.htmlTechArticlePHP strcmp, strnatcmp, strncmp function differences, strnatcmpstrncmp string comparison shows that when s1s2, return a negative number when s1 = s2, return a value = 0 when s1s2, return a positive number 1? Php 2 $ st...

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.