1. size_t find (const string& str, size_t pos = 0)
说明:
说明:在字符串中搜索由其参数str指定的序列的第一个匹配项。如果指定了pos,则搜索只包括位置pos处或位置之后的字符,忽略任何可能出现的包含位置之前的字符的匹配项。每当搜索多个字符时,必须匹配整个序列,这与 **find_first_of ** 不同。返回 第一个匹配项的第一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考:find函数:http://www.cplusplus.com/reference/string/string/find/
例子:
// string::find
#include <iostream> // std::cout
#include <string> // std::string
int main ()
{
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';
return 0;
}
输出:
2. size_t rfind (const string& str, size_t pos = npos)
说明:
说明:在字符串中搜索由其参数str指定的序列的最后一个匹配项。如果指定了pos,则搜索只包括位置pos或位置pos之前开始的字符序列,忽略从pos之后开始的任何可能的匹配。每当搜索多个字符时,必须匹配整个序列。返回 最后一个匹配的第一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考:find函数:http://www.cplusplus.com/reference/string/string/rfind/
例子:
// string::rfind
#include <iostream>
#include <string>
#include <cstddef>
int main ()
{
std::string str ("The sixth sick sheik's sixth sheep's sick.");
std::string key ("sixth");
std::size_t found = str.rfind(key);
if (found!=std::string::npos)
str.replace (found,key.length(),"seventh");
std::cout << str << '\n';
return 0;
}
输出:
3. size_t find_first_of (const string& str, size_t pos = 0)
说明:
说明:在字符串中搜索与其参数str中指定的任意字符匹配的第一个字符。如果指定了pos,则搜索只包括pos位置或位置之后的字符,忽略pos之前可能出现的任何字符。是str中的任意一个字符匹配就足够了(不是所有字符)。匹配的第一个字符的位置。如果没有找到则返回string::npos。
参考:find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/
例子:
// string::find_first_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
int main ()
{
std::string str ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found = str.find_first_of("aeiou");
while (found!=std::string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
std::cout << str << '\n';
return 0;
}
输出:
4.size_t find_last_of (const string& str, size_t pos = npos)
说明:
说明:在字符串中搜索与其参数str中指定的任意字符匹配的最后一个字符。如果指定了pos,则搜索只包括位置pos或位置pos之前的字符,忽略pos之后可能出现的任何字符。是str中的任意一个字符匹配就足够了(不是所有字符)。匹配的最后一个字符的位置。如果没有找到匹配项,该函数将返回string::npos。
参考: find_last_of函数:http://www.cplusplus.com/reference/string/string/find_last_of/
例子:
// string::find_last_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
void SplitFilename (const std::string& str)
{
std::cout << "Splitting: " << str << '\n';
std::size_t found = str.find_last_of("/\\");
std::cout << " path: " << str.substr(0,found) << '\n';
std::cout << " file: " << str.substr(found+1) << '\n';
}
int main ()
{
std::string str1 ("/usr/bin/man");
std::string str2 ("c:\\windows\\winhelp.exe");
SplitFilename (str1);
SplitFilename (str2);
return 0;
}
输出:
注意:
find_first_of 和 find_last_of 匹配的是指定str中任意一字符,find 和 rfind 匹配的是指定str整个序列