C++ Reference: Standard C++ Library reference: C Library: cstring: strpbrk

发布于:2022-10-29 ⋅ 阅读:(292) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/cstring/strpbrk/

函数 
<cstring>
strpbrk
const char * strpbrk ( const char * str1, const char * str2 );      
char * strpbrk ( char * str1, const char * str2 );
定位字符串中的字符
返回指向str1中第一个出现的str2字符的指针,如果没有匹配则返回空指针。
查找不包括任何字符串的结束空字符,而是结束于此。

形参 
str1
要扫描的C字符串。
str2
包含要匹配的字符的C字符串。

返回值
指向str1中str2中第一个出现的字符的指针,如果str1中在终止的空字符之前没有str2中的任何字符,则为空指针。
如果str1中没有str2中的字符,则返回空指针。

可移植性
在C语言中,这个函数只被声明为: 
strpbrk(const char *, const char *);
而不是C++中提供的两个重载版本。

用例
/* 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;

输出:

另请参考
strcspn    Get span until character in string (function)
strchr    Locate first occurrence of character in string (function)
strrchr    Locate last occurrence of character in string (function)
memchr    Locate character in block of memory (function) 

本文含有隐藏内容,请 开通VIP 后查看