特有函式
功能:比較字元串s1和s2,但不區分字母的大小寫。
當s1
當s1=s2時,返回值=0
當s1>s2時,返回值>0
舉例:
// stricmp.c
#include
#include
main( )
{char *s1="Hello, Programmers!";char *s2="Hello, programmers!";int r;clrscr( );r=stricmp(s1,s2);if(!r)printf("s1 and s2 are identical");elseif(r<0)printf("s1 less than s2");elseprintf("s1 greater than s2");getchar( );return 0;}
C/C++中,可以用_stricmp等函式處理字元串大小寫不敏感問題。
下面是MSDN上的介紹
_stricmp, _wcsicmp, _mbsicmp
Perform a lowercase comparison of strings.
int _stricmp( const char *string1, const char *string2 );
int _wcsicmp( const wchar_t *string1, const wchar_t *string2 );
int _mbsicmp( const unsigned char *string1, const unsigned char_t *string2 );
Routine Required Header Compatibility
_stricmp Win 95, Win NT
_wcsicmp or Win 95, Win NT
_mbsicmp Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
The return value indicates the relation of string1 to string2 as follows.
Return Value Description
< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2
On an error, _mbsicmp returns _NLSCMPERROR, which is defined in STRING.H and MBSTRING.H.
Parameters
string1, string2
Null-terminated strings to compare
Remarks
The _stricmp function lexicographically compares lowercase versions of string1 and string2 and returns a value indicating their relationship. _stricmp differs from _stricoll in that the _stricmp comparison is not affected by locale, whereas the _stricoll comparison is according to the LC_COLLATE category of the current locale. For more information on the LC_COLLATE category, see setlocale.
The _
strcmpi function is equivalent to _stricmp and is provided for backward compatibility only.
_wcsicmp and _mbsicmp are wide-character and multibyte-character versions of _stricmp. The arguments and return value of _wcsicmp are wide-character strings; those of _mbsicmp are multibyte-character strings. _mbsicmp recognizes multibyte-character sequences according to the current multibyte code page and returns _NLSCMPERROR on an error. (For more information, see Code Pages.) These three functions behave identically otherwise.
_wcsicmp and
wcscmp behave identically except that wcscmp does not convert its arguments to lowercase before comparing them. _mbsicmp and _mbscmp behave identically except that _mbscmp does not convert its arguments to lowercase before comparing them.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcsicmp _stricmp _mbsicmp _wcsicmp
Example
/* STRCMP.C */
#include
#include
char string1[ ] = "The quick brown dog jumps over the lazy fox";
char string2[ ] = "The QUICK brown dog jumps over the lazy fox";
void main( void )
{char tmp[20];int result;/* Case sensitive */printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );result = strcmp( string1, string2 );if( result>0 )strcpy( tmp, "greater than" );else if( result<0 )strcpy( tmp, "less than" );elsestrcpy( tmp, "equal to" );printf( "\tstrcmp: String 1 is %s string 2\n", tmp );/* Case insensitive (could use equivalent _stricmp) */result = _stricmp( string1, string2 );if( result>0 )strcpy( tmp, "greater than" );else if( result<0 )strcpy( tmp, "less than" );elsestrcpy( tmp, "equal to" );printf( "\t_stricmp: String 1 is %s string 2\n", tmp );}
Output
Compare strings:
The quick brown dog jumps over the lazy fox
The QUICK brown dog jumps over the lazy fox
strcmp: String 1 is greater than string 2
_stricmp: String 1 is equal to string 2
String Manipulation Routines
運行環境
linux下具有相同功能的函式是strcasecmp,包含在頭檔案strings.h下。