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

std::strstr

Defined in header <cstring>

const char* strstr( const char* str, const char* target );

char* strstr( char* str, const char* target );

查找字节字符串的第一个匹配项。target所指向的字节字符串中str不对终止空字符进行比较。

参数

str

-

pointer to the null-terminated byte string to examine

target

-

pointer to the null-terminated byte string to search for

返回值

中找到的子字符串的第一个字符的指针。str,或NULL如果找不到这样的角色。如果target指向空字符串,str会被归还。

二次

代码语言:javascript
复制
#include <iostream>
#include <cstring>
 
int main()
{
    const char *str = "Try not. Do, or do not. There is no try.";
    const char *target = "not";
    const char *result = str;
 
    while ((result = std::strstr(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 'not' starting at 'not. Do, or do not. There is no try.'
Found 'not' starting at 'not. There is no try.'

二次

另见

find

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

wcsstr

finds the first occurrence of a wide string within another wide string (function)

strchr

finds the first occurrence of a character (function)

strrchr

finds the last occurrence of a character (function)

c strstr文件

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

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

扫码关注腾讯云开发者

领取腾讯云代金券