當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ wcscspn()用法及代碼示例

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


相關用法


注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wcscspn() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。