std::strpbrk
来自cppreference.com
| 定义于头文件 <cstring>
|
||
| const char* strpbrk( const char* dest, const char* breakset ); |
||
| char* strpbrk( char* dest, const char* breakset ); |
||
在 dest 所指向的空终止字节串中,扫描来自 breakset 所指向的空终止字节串的任何字符,并返回指向该字符的指针。
参数
| dest | - | 指向要分析的空终止字节字符串的指针 |
| breakset | - | 指向含要搜索的字符的空终止字节字符串的指针 |
返回值
指向 dest 中首个亦在 breakset 中的字符的指针,或若这种字符不存在则为空指针。
注解
名称代表“字符串指针打断 (string pointer break) ”,因为它返回指向首个分隔符(“打断”)的指针。
示例
运行此代码
#include <iostream> #include <iomanip> #include <cstring> int main() { const char* str = "hello world, friend of mine!"; const char* sep = " ,!"; unsigned int cnt = 0; do { str = std::strpbrk(str, sep); // 寻找分隔符 std::cout << std::quoted(str) << '\n'; if(str) str += std::strspn(str, sep); // 跳过分隔符 ++cnt; // 增加词计数 } while(str && *str); std::cout << "There are " << cnt << " words\n"; }
输出:
" world, friend of mine!" ", friend of mine!" " of mine!" " mine!" "!" There are 5 words
参阅
| 返回仅由另一字节字符串中找不到的字符组成的最大起始段的长度 (函数) | |
| 寻找字节字符串中的下个记号 (函数) | |
| 寻找字符的首次出现 (函数) | |
| 在一个宽字符串中,寻找另一宽字符串中任何字符的首个位置 (函数) |