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

std::basic_string::find

size_type find( const basic_string& str, size_type pos = 0 ) const

(1)

size_type find( const CharT* s, size_type pos, size_type count ) const;

(2)

size_type find( const CharT* s, size_type pos = 0 ) const;

(3)

size_type find( CharT ch, size_type pos = 0 ) const;

(4)

size_type find( std::basic_string_view<CharT, Traits> sv, size_type pos = 0) const

(5)

(since C++17)

查找等于给定字符序列的第一个子字符串。搜索开始于pos,即发现的子字符串不能从前面的位置开始。pos...

1%29查找第一个子字符串等于str...

2%29查找第一个子字符串等于第一个子字符串。count所指向的字符串的字符。s...s可以包含空字符。

3%29查找第一个子字符串,该子字符串等于s字符串的长度由第一个空字符决定。

4%29找到第一个字符ch%29以下的形式规则将%28视为单字符子字符串。

5%29查找第一个子字符串与String视图相等。sv...

形式上,子字符串str据说是发现就位xpos如果以下所有内容都是正确的:

  • xpos >= pos
  • xpos + str.size() <= size()
  • 所有职位nstr,,,Traits::eq(at(xpos+n), str.at(n))

特别是,这意味着。

  • 只有在以下情况下才能找到子字符串pos <= size() - str.size()
  • 空子字符串位于pos当且仅当pos <= size()
  • 对于非空子字符串,如果pos >= size(),函数总是返回npos...

参数

str

-

string to search for

pos

-

position at which to start the search

count

-

length of substring to search for

s

-

pointer to a character string to search for

ch

-

character to search for

sv

-

std::basic_string_view to search for

返回值

找到的子字符串或npos如果找不到这样的子字符串。

例外

1-4) (none)

(until C++11)

1,4) noexcept specification: noexcept 2,3) (none)

(since C++11)(until C++14)

1) noexcept specification: noexcept 2,3,4) (none)

(since C++14)

5) noexcept specification: noexcept

(since C++17)

二次

代码语言:javascript
复制
#include <string>
#include <iostream>
 
void print(std::string::size_type n, std::string const &s)
{
    if (n == std::string::npos) {
        std::cout << "not found\n";
    } else {
        std::cout << "found: " << s.substr(n) << '\n';
    }
}
 
int main()
{
    std::string::size_type n;
    std::string const s = "This is a string";
 
    // search from beginning of string
    n = s.find("is");
    print(n, s);
 
    // search from position 5
    n = s.find("is", 5);
    print(n, s);
 
    // find a single character
    n = s.find('a');
    print(n, s);
 
    // find a single character
    n = s.find('q');
    print(n, s);
}

二次

产出:

二次

代码语言:javascript
复制
found: is is a string
found: is a string
found: a string
not found

二次

另见

strstr

finds the first occurrence of a substring of characters (function)

wcsstr

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

strchr

finds the first occurrence of a character (function)

wcschr

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

rfind

find the last occurrence of a substring (public member function)

find_first_of

find first occurrence of characters (public member function)

find_first_not_of

find first absence of characters (public member function)

find_last_of

find last occurrence of characters (public member function)

find_last_not_of

find last absence of characters (public member function)

search

searches for a range of elements (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券