目录

find

描述 (Description)

它在字符串中搜索由其参数指定的序列的第一次出现。

声明 (Declaration)

以下是std :: string :: find的声明。

size_t find (const string& str, size_t pos = 0) const;

C++11

size_t find (const string& str, size_t pos = 0) const noexcept;

C++14

size_t find (const string& str, size_t pos = 0) const noexcept;

参数 (Parameters)

  • str - 它是一个字符串对象。

  • len - 用于复制字符。

  • pos - 要复制的第一个字符的位置。

返回值 (Return Value)

没有

异常 (Exceptions)

如果抛出异常,则字符串中没有变化。

例子 (Example)

在下面的示例中,std :: string :: find。

#include <iostream>
#include <string>
int main () {
   std::string str ("sairamkrishna Mammahe is a tech person in iowiki.com.");
   std::string str2 ("needle");
   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';
   str.replace(str.find(str2),str2.length(),"preposition");
   std::cout << str << '\n';
   return 0;
}

样本输出应该是这样的 -

Period found at: 56
↑回到顶部↑
WIKI教程 @2018