首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::strchr

Defined in header <cstring>

const char* strchr( const char* str, int ch );

char* strchr( char* str, int ch );

查找字符的第一次出现。static_cast<char>(ch)所指向的字节字符串中str...

终止空字符被认为是字符串的一部分。

参数

str

-

pointer to the null-terminated byte string to be analyzed

ch

-

character to search for

返回值

中找到的字符的指针。str,如果没有找到这样的字符,则为空指针。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstring>
 
int main()
{
  const char *str = "Try not. Do, or do not. There is no try.";
  char target = 'T';
  const char *result = str;
 
  while ((result = std::strchr(result, target)) != NULL) {
    std::cout << "Found '" << target
              << "' starting at '" << result << "'\n";
 
    // Increment result, otherwise we'll find target at the same location
    ++result;
  }
}

二次

产出:

二次

代码语言:javascript
复制
Found 'T' starting at 'Try not. Do, or do not. There is no try.'
Found 'T' starting at 'There is no try.'

二次

另见

find

find characters in the string (public member function of std::basic_string)

wcschr

finds the first occurrence of a wide character in a wide string (function)

strrchr

finds the last occurrence of a character (function)

strpbrk

finds the first location of any character from a set of separators (function)

C.strchr文件

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券