strcoll ()- 比较字符串

格式

#include <string.h>
int strcoll(const char *string1, const char *string2);

语言级别

ANSI

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_COLLATE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

strcoll() 函数使用程序的语言环境指定的整理顺序来比较两个字符串。

返回值

strcoll() 函数返回一个值,指示字符串之间的关系,如下所示:
含义
小于 0 string1 小于 string2
0 string1 等效于 string2
大于 0 string1 大于 string2
如果 strcoll() 不成功,那么将更改 errno。 errno 的值可以设置为 EINVAL ( string1string2 自变量包含在当前语言环境中不可用的字符)。

示例

此示例比较了使用 strcoll()传递到 main() 的两个字符串:
#include <stdio.h>
#include <string.h>
 
int main(int argc, char ** argv)
{
  int  result;
 
  if ( argc != 3 )
  {
    printf( "Usage: %s string1 string2\n", argv[0] );
  }
  else
  {
 
    result = strcoll( argv[1], argv[2] );
 
    if ( result == 0 )
      printf( "\"%s\" is identical to \"%s\"\n", argv[1], argv[2] );
    else if ( result < 0 )
      printf( "\"%s\" is less than \"%s\"\n", argv[1], argv[2] );
    else
      printf( "\"%s\" is greater than \"%s\"\n", argv[1], argv[2] );
  }
}
 
/******************  If the input is the strings  ***********************
****************  "firststring" and "secondstring",  ********************
******************  then the expected output is:  *****************
 
"firststring" is less than "secondstring"
*/

相关信息