Examples
The following example uses the wcschr subroutine to locate the first occurrence of a wide character in a wide character string:
#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 */
}
}
The following example uses the wcsrchr subroutine to locate the last occurrence of a wide character in a wide character string:
#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 */
}
}
The following example uses the wcspbrk subroutine to locate the first occurrence of several wide characters in a wide character string:
#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 */
}
}
The following example uses the wcsspn subroutine to determine the number of wide characters in the initial segment of a wide character string segment:
#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.
*/
}
The following example uses the wcscspn subroutine to determine the number of wide characters not in a wide character string segment:
#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.
*/
}
The following example uses the wcswcs subroutine to locate the first occurrence of a wide character string within another wide character string:
#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.
*/
}
}
The following example uses the wcstok subroutine to tokenize a wide character string:
#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" */
}