C /C++中的wcscspn()函数在给定的宽字符串_1中搜索字符串_2的宽字符的第一个匹配项。它返回第一次出现的宽字符数。搜索包括终止的空宽字符。因此,如果在string_1中找不到string_2的任何字符,则该函数将返回string_1的长度。
用法:
size_t wcscspn( const wchar_t* string_1, const wchar_t* string_2 )
参数:该函数接受两个强制性参数,如下所述:
- string_1:指定要搜索的字符串
 - string_2:指定包含要搜索的字符的字符串
 
返回值:该函数返回两个值,如下所示:
- 它返回string_1中出现的任何宽字符第一次出现之前的string_2中的宽字符数目。
 - 如果在string_1中找不到string_2中的宽字符,则返回string_1的长度
 
以下示例程序旨在说明上述函数:
程序1:
// C++ program to illustrate 
// wcscspn() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // string to be scanned 
    wchar_t string_1[] = L"geeksforgeeks012345"; 
  
    // string containing the character to match 
    wchar_t string_2[] = L"0123456789"; 
  
    // scanning the strings 
    int last = wcscspn(string_1, string_2); 
  
    // print the position of a matched character 
    if (last > wcslen(string_1)) 
        wcout << string_1 << L" Didn't match any character"; 
    else
        wcout << L"Occurrence of a character in -> \n"
              << string_1 << " is at position:" << last; 
    return 0; 
}
输出:
Occurrence of a character in -> geeksforgeeks012345 is at position:13
程序2:
// C++ program to illustrate 
// wcscspn() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // string to be scanned 
    wchar_t string_1[] = L"GFG"; 
  
    // string containing the character to match 
    wchar_t string_2[] = L"909090909"; 
  
    // scanning the strings 
    int last = wcscspn(string_1, string_2); 
  
    // print the position of a matched character 
    if (last > wcslen(string_1)) 
        wcout << string_1 << L" does not contain numbers"; 
    else
        wcout << L"Length of the string -> "
              << string_1 << " is:" << last; 
    return 0; 
}
输出:
Length of the string -> GFG is:3
相关用法
- C++ fma()用法及代码示例
 - C++ div()用法及代码示例
 - C++ log()用法及代码示例
 - C++ iswxdigit()用法及代码示例
 - C语言 strrev()用法及代码示例
 - C++ towlower()用法及代码示例
 - C++ array get()用法及代码示例
 - C++ array at()用法及代码示例
 - C++ iswspace()用法及代码示例
 
注:本文由纯净天空筛选整理自AmanSrivastava1大神的英文原创作品 wcscspn() function in C/C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
