一、strcmp
Compares string1 and string2 to determine the alphabetic order.
int strcmp ( const char *string1, const char *string2 ); //注意是int
string1 |
The first string that is compared. |
string2 |
The second string that is compared. |
strcmp compares string1 and string2 to determine the alphabetic order.
返回值:
Returns a value indicating the lexicographical relation between the strings:
Return value |
Descrīption |
<0 |
string1 is less than string2 |
0 |
string1 is the same as string2 |
>0 |
string1 is greater than string2 |
二、stricmp
(与strcmp的区别仅为忽略大小写)
Performs a case-insensitive comparison of two strings.
int stricmp ( const char *string1, const char *string2 );
string1 |
The first string for comparison. |
string2 |
The second string for comparison. |
stricmp performs a case-insensitive comparison of two strings.
Returns a value indicating the lexicographical relation between the strings:
Return value |
Descrīption |
<0 |
string1 is less than string2 |
0 |
string1 is the same as string2 |
>0 |
string1 is greater than string2 |
脚本说明:
The following example compares two strings, string1 and string2, which are identical except for the word "quick" which is lowercase in string1 and uppercase in string2. strcmp, which is case-sensitive, returns an unequal comparison. stricmp, which ignores case, returns an equal one.
下面的例子比较了两个字符串,string1和string2,除了单词"quick"在string1中为小写字母,在string2中为大写字母外,其他字母都是相同的。strcmp,是大小写敏感的,这个函数会返回一个不相同的结果。stricmp,会忽略大小写,返回相同的结果
#include "web_api.h"
Action() {
int result;
char tmp[20];
char string1[] = "The quick brown dog jumps over the lazy fox";
char string2[] = "The QUICK brown dog jumps over the lazy fox";
result = strcmp( string1, string2 ); /* Case-sensitive comparison */
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
lr_output_message( "strcmp: String 1 is %s string 2", tmp );
result = stricmp( string1, string2 ); /* Case-insensitive comparison */
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
lr_output_message( "stricmp: String 1 is %s string 2", tmp );
return 0;
}
结果:
Action.c(20): strcmp: String 1 is greater than string 2
Action.c(31): stricmp: String 1 is equal to string 2