示例
下面的示例使用 wcschr 子例程查找在一段宽字符字符串中第一次出现的一个宽字符:
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, wc, *pws;
int retval;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let wc point to the wide character to search for.
**
*/
pws = wcschr(pwcs1, wc);
if (pws == (wchar_t )NULL ){
/* wc does not occur in pwcs1 */
}else{
/* pws points to the location where wc is found */
}
}
下面的示例使用 wcsrchr 子例程查找在一段宽字符字符串中最后一次出现的一个宽字符:
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, wc, *pws;
int retval;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let wc point to the wide character to search for.
**
*/
pws = wcsrchr(pwcs1, wc);
if (pws == (wchar_t )NULL ){
/* wc does not occur in pwcs1 */
}else{
/* pws points to the location where wc is found */
}
}
下面的示例使用 wcspbrk 子例程查找在一段宽字符字符串中第一次出现的多个宽字符:
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, *pwcs2, *pws;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let pwcs2 be initialized to the wide character string
** that contains wide characters to search for.
*/
pws = wcspbrk(pwcs1, pwcs2);
if (pws == (wchar_t )NULL ){
/* No wide character from pwcs2 is found in pwcs1 */
}else{
/* pws points to the location where a match is found */
}
}
下面的示例使用 wcsspn 子例程确定一段宽字符字符串中最初一段中的字符数:
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, *pwcs2;
size_t count;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let pwcs2 be initialized to the wide character string
** that contains wide characters to search for.
*/
count = wcsspn(pwcs1, pwcs2);
/*
** count contains the length of the segment.
*/
}
下面的示例使用 wcscspn 子例程确定在一段宽字符字符串中不存在的宽字符的数目。
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, *pwcs2;
size_t count;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let pwcs2 be initialized to the wide character string
** that contains wide characters to search for.
*/
count = wcscspn(pwcs1, pwcs2);
/*
** count contains the length of the segment consisting
** of characters not in pwcs2.
*/
}
下面的示例使用 wcswcs 子例程查找在另一段宽字符字符串中第一次出现的一段宽字符字符串:
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1, *pwcs2, *pws;
(void)setlocale(LC_ALL, "");
/*
** Let pwcs1 point to a wide character null terminated string.
** Let pwcs2 be initialized to the wide character string
** that contains wide characters sequence to locate.
*/
pws = wcswcs(pwcs1, pwcs2);
if (pws == (wchar_t)NULL){
/* wide character sequence pwcs2 is not found in pwcs1 */
}else{
/*
** pws points to the first occurrence of the sequence
** specified by pwcs2 in pwcs1.
*/
}
}
下面的例子使用 wcstok 子例程将一段宽字符字符串分拆成多段子串。
#include <string.h>
#include <locale.h>
#include <stdlib.h>
main()
{
wchar_t *pwcs1 = L"?a???b,,,#c";
wchar_t *pwcs;
(void)setlocale(LC_ALL, "");
pwcs = wcstok(pwcs1, L"?");
/* pws points to the token: L"a" */
pwcs = wcstok((wchar_t *)NULL, L",");
/* pws points to the token: L"??b" */
pwcs = wcstok((wchar_t *)NULL, L"#,");
/* pws points to the token: L"c" */
}