Namespaces
Variants

wcsncmp

From cppreference.net
< c ‎ | string ‎ | wide
定义于头文件 <wchar.h>
int wcsncmp ( const wchar_t * lhs, const wchar_t * rhs, size_t count ) ;
(C95 起)

最多比较两个以空字符结尾的宽字符串中的 count 个宽字符。该比较按字典序进行。

结果的符号取决于被比较字符串中第一对不同的宽字符值之间的差值符号。

如果 lhs rhs 不是指向以空字符结尾的字符串的指针,则行为是未定义的。

目录

参数

lhs, rhs - 指向要比较的以空字符结尾的宽字符串的指针
count - 要比较的最大字符数

返回值

lhs 在字典序中出现在 rhs 之前,则返回负值。

lhs rhs 比较相等时返回零。

正值表示 lhs 在字典序中出现在 rhs 之后。

注释

此函数与 wcscoll wcsxfrm 不同,不依赖于区域设置。

示例

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
void demo(const wchar_t *lhs, const wchar_t *rhs, int sz)
{
    int rc = wcsncmp(lhs, rhs, sz);
    if(rc == 0)
        printf("First %d characters of [%ls] equal [%ls]\n", sz, lhs, rhs);
    else if(rc < 0)
        printf("First %d characters of [%ls] precede [%ls]\n", sz, lhs, rhs);
    else if(rc > 0)
        printf("First %d characters of [%ls] follow [%ls]\n", sz, lhs, rhs);
}
int main(void)
{
    const wchar_t *str1 = L"안녕하세요";
    const wchar_t *str2 = L"안녕히 가십시오";
    setlocale(LC_ALL, "en_US.utf8");
    demo(str1, str2, 5);
    demo(str2, str1, 8);
    demo(str1, str2, 2);
}

输出:

First 5 characters of [안녕하세요] precede [안녕히 가십시오]
First 8 characters of [안녕히 가십시오] follow [안녕하세요]
First 2 characters of [안녕하세요] equal [안녕히 가십시오]

参考文献

  • C11 标准 (ISO/IEC 9899:2011):
  • 7.29.4.4.3 wcsncmp 函数 (第434页)
  • C99 标准 (ISO/IEC 9899:1999):
  • 7.24.4.4.3 wcsncmp 函数 (第380页)

参见

(C95)
比较两个宽字符串
(函数)
(C95)
比较两个数组中的指定数量宽字符
(函数)
(C95)
根据当前本地环境比较两个宽字符串
(函数)
C++ 文档 for wcsncmp