Namespaces
Variants
Views
Actions

std::basic_string::find

From cppreference.com
< cpp‎ | string‎ | basic string
Revision as of 07:47, 4 February 2013 by Nate (Talk | contribs)

 
 
 
std::basic_string
 

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
size_type find( const basic_string& str, size_type pos = 0 ) const;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
size_type find( const CharT* s, size_type pos, size_type count ) const;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
size_type find( const CharT* s, size_type pos = 0 ) const;
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
size_type find( CharT ch, size_type pos = 0 ) const;
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Finds the first substring equal to the given character sequence. Search begins at pos, i.e. the found substring must not begin in a position preceding pos.

1) Finds the first substring equal to str.

2) Finds the first substring equal to the first count characters of the character string pointed to by s. s can include null characters.

3) Finds the first substring equal to the character string pointed to by s. The length of the string is determined by the first null character.

4) Finds the first character ch.

Contents

Parameters

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 seach for
ch - character to search for

Return value

position of the first character of the found substring or Template:rlpt if no such substring is found.

Example

#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);
}

Output:

found: is is a string
found: is a string
found: a string
not found

See also

Template:cpp/string/byte/dcl list strstrTemplate:cpp/string/wide/dcl list wcsstrTemplate:cpp/string/byte/dcl list strchrTemplate:cpp/string/wide/dcl list wcschrTemplate:cpp/string/basic string/dcl list rfindTemplate:cpp/string/basic string/dcl list find first ofTemplate:cpp/string/basic string/dcl list find first not ofTemplate:cpp/string/basic string/dcl list find last ofTemplate:cpp/string/basic string/dcl list find last not ofTemplate:cpp/algorithm/dcl list search