函式
<cstring>

strpbrk

const char * strpbrk ( const char * str1, const char * str2 );      char * strpbrk (       char * str1, const char * str2 );
在 str1 中定位字元
str1 中返回指向 str2 中任何字元的第一次出現的指標,如果不存在匹配項,則返回空指標。

搜尋不包括任一字串的終止空字元,但以此處為終點。

引數

str1
要被掃描的 C 字串。
str2
包含要匹配的字元的 C 字串。

返回值

指向 str1str2 中任何字元的第一次出現的指標,如果 str2 中的字元在終止空字元之前未在 str1 中找到,則返回空指標。
如果 str2 中的字元均未出現在 str1 中,則返回空指標。

可移植性

在 C 語言中,此函式僅宣告為

char * strpbrk ( const char *, const char * );

而不是 C++ 中提供的兩個過載版本。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* strpbrk example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char key[] = "aeiou";
  char * pch;
  printf ("Vowels in '%s': ",str);
  pch = strpbrk (str, key);
  while (pch != NULL)
  {
    printf ("%c " , *pch);
    pch = strpbrk (pch+1,key);
  }
  printf ("\n");
  return 0;
}

輸出

Vowels in 'This is a sample string': i i a a e i 


另見