string类的find()函数总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shujh_sysu/article/details/52026108
                                        <div class="markdown_views">
            <p>string类的头文件提供了很多搜索相关的函数比如find()函数及其变体。这使得我们可以以多种不同的方式在字符串中搜索给定的子字符串或字符。但是对于初学者来讲,经常被这些长相类似的函数所混淆。 <br>

下面总结了string类的find相关函数:

1、find():

find函数有四种变体:

方法原型 描述
size_type find(const string & str, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0, size_type n) const 从字符串的pos位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char ch, size_type pos = 0) const 从字符串的pos位置开始,查找字符ch。如果找到,则返回该子字符串首次出现的位置;否则,返回string::npos

P.S.string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值。

string库还提供了相关的方法:rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()。他们的重载函数特征标都与find()方法相同。

2、rfind():

原型:

size_type rfind(const string & str, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos, size_type n) const;
size_type rfind(const char ch, size_type pos = npos) const;
   
   
  • 1
  • 2
  • 3
  • 4

rfind()方法查找子字符串或字符最后一次出现的位置。

3、find_first_of():

原型:

size_type find_first_of(const string & str, size_type pos = 0) const;
size_type find_first_of(const char * s, size_type pos, size_type n) const;
size_type find_first_of(const char * s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;
   
   
  • 1
  • 2
  • 3
  • 4

find_first_of()方法在字符串中查找参数中任何一个字符首次出现的位置。例如,下面的语句返回r在”cobra”中的位置(即索引3),因为这个”hark”中各个字母在”cobra”首次出现的位置:

string snake1 = "cobra";
int where = snake1.find_first_of("hark");
   
   
  • 1
  • 2

4、find_last_of():

原型:

size_type find_last_of(const string & str, size_type pos = npos) const;
size_type find_last_of(const char * s, size_type pos, size_type n) const;
size_type find_last_of(const char * s, size_type pos = npos) const;
size_type find_last_of(char c, size_type pos = npos) const;
   
   
  • 1
  • 2
  • 3
  • 4

find_last_of()方法在字符串中查找参数中任何一个字符最后一次出现的位置。

5、find_first_not_of():

原型:

size_type find_first_not_of(const string & str, size_type pos = 0) const;
size_type find_first_not_of(const char * s, size_type pos, size_type n) const;
size_type find_first_not_of(const char * s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;
   
   
  • 1
  • 2
  • 3
  • 4

find_first_not_of()方法在字符串中查找第一个不包含在参数中的字符,因此下面的语句返回c在”cobra”中的位置,因为”hark”中没有c:

string snake1 = "cobra";
int where = snake1.find_first_not_of("hark");
   
   
  • 1
  • 2

6、find_last_not_of():

原型:

size_type find_last_not_of(const string & str, size_type pos = npos) const;
size_type find_last_not_of(const char * s, size_type pos, size_type n) const;
size_type find_last_not_of(const char * s, size_type pos = npos) const;
size_type find_last_not_of(char c, size_type pos = npos) const;
   
   
  • 1
  • 2
  • 3
  • 4

find_last_not_of()方法在字符串中查找最后一个不包含在参数中的字符。



版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shujh_sysu/article/details/52026108
                                        <div class="markdown_views">
            <p>string类的头文件提供了很多搜索相关的函数比如find()函数及其变体。这使得我们可以以多种不同的方式在字符串中搜索给定的子字符串或字符。但是对于初学者来讲,经常被这些长相类似的函数所混淆。 <br>

下面总结了string类的find相关函数:

1、find():

find函数有四种变体:

方法原型 描述
size_type find(const string & str, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0) const 从字符串的pos位置开始,查找子字符串s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char * s, size_type pos = 0, size_type n) const 从字符串的pos位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回string::npos
size_type find(const char ch, size_type pos = 0) const 从字符串的pos位置开始,查找字符ch。如果找到,则返回该子字符串首次出现的位置;否则,返回string::npos

P.S.string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值。

string库还提供了相关的方法:rfind(),find_first_of(),find_last_of(),find_first_not_of(),find_last_not_of()。他们的重载函数特征标都与find()方法相同。

2、rfind():

原型:

size_type rfind(const string & str, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos) const;
size_type rfind(const char * s, size_type pos = npos, size_type n) const;
size_type rfind(const char ch, size_type pos = npos) const;
 
 
  • 1
  • 2
  • 3
  • 4

rfind()方法查找子字符串或字符最后一次出现的位置。

3、find_first_of():

原型:

size_type find_first_of(const string & str, size_type pos = 0) const;
size_type find_first_of(const char * s, size_type pos, size_type n) const;
size_type find_first_of(const char * s, size_type pos = 0) const;
size_type find_first_of(char c, size_type pos = 0) const;
 
 
  • 1
  • 2
  • 3
  • 4

find_first_of()方法在字符串中查找参数中任何一个字符首次出现的位置。例如,下面的语句返回r在”cobra”中的位置(即索引3),因为这个”hark”中各个字母在”cobra”首次出现的位置:

string snake1 = "cobra";
int where = snake1.find_first_of("hark");
 
 
  • 1
  • 2

4、find_last_of():

原型:

size_type find_last_of(const string & str, size_type pos = npos) const;
size_type find_last_of(const char * s, size_type pos, size_type n) const;
size_type find_last_of(const char * s, size_type pos = npos) const;
size_type find_last_of(char c, size_type pos = npos) const;
 
 
  • 1
  • 2
  • 3
  • 4

find_last_of()方法在字符串中查找参数中任何一个字符最后一次出现的位置。

5、find_first_not_of():

原型:

size_type find_first_not_of(const string & str, size_type pos = 0) const;
size_type find_first_not_of(const char * s, size_type pos, size_type n) const;
size_type find_first_not_of(const char * s, size_type pos = 0) const;
size_type find_first_not_of(char c, size_type pos = 0) const;
 
 
  • 1
  • 2
  • 3
  • 4

find_first_not_of()方法在字符串中查找第一个不包含在参数中的字符,因此下面的语句返回c在”cobra”中的位置,因为”hark”中没有c:

string snake1 = "cobra";
int where = snake1.find_first_not_of("hark");
 
 
  • 1
  • 2

6、find_last_not_of():

原型:

size_type find_last_not_of(const string & str, size_type pos = npos) const;
size_type find_last_not_of(const char * s, size_type pos, size_type n) const;
size_type find_last_not_of(const char * s, size_type pos = npos) const;
size_type find_last_not_of(char c, size_type pos = npos) const;
 
 
  • 1
  • 2
  • 3
  • 4

find_last_not_of()方法在字符串中查找最后一个不包含在参数中的字符。

猜你喜欢

转载自blog.csdn.net/weixin_42255385/article/details/82291432