String 中字符(串)查找

  • 格式:doc
  • 大小:121.50 KB
  • 文档页数:11

String的用法String类仅仅查找操作就为我们提供了6种查找函数,每种函数以不同形式的find命名。

find、r find、find_first_of 、find_first_not_of、find_last_of 、find_last_not_of这些都是const成员函数,const 也就是说,它们只是为了针对某种用途去寻找子串的位置,并不能够改变它们所作用的那个串的值。

这些操作全都返回string::size_type类型的值,也就是说,返回的结果是一个unsigned值(即,无符号整形)。

要么是以下标形式查找匹配所发生的位置(也就是返回当前查找到的子串下标);或者返回一个名为string::npos 类型的值,说明查找没有匹配,也就是找不到你所查找的字符。

就这么两种形式,要么是返回下标,要么返回npos。

每个查找操作都有4个重载版本,每个版本使用不同的参数形式。

基本上,这些操作的不同之处在于查找的到底是单个字符,还是另一个string字符串,或者c风格的以空字符结束的字符串,还是用字符数组给出的特定数目的字符集合。

比如说:size_type find(const basic_string &s , size_type i=0) const;const修饰函数参数是它最广泛的一种用途,它表示函数体中不能修改参数的值(包括参数本身的值或者参数其中包含的值)。

注:basic_string::nposAn unsigned integral value initialized to –1 that indicates either "not found" or "all remaining characters" when a search function fails.static const size_type npos = -1;1、Find 查找子串第一次出现的位置Searches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters最简单的查找操作就是find函数了。

用于寻找实参指定的内容。

如果能找到的话,则返回第一次匹配的下标值;如果找不到,则返回npos。

在此,我们首先定义一个字符串。

String s(“hello world”); //定义一个string变量sString::size_type pos= s.find(args); //args –>参数if (pos!= string::npos )cout<<pos;Msdn:find method1.4种重载形式:2.参数定义:_Ch The character value for which the member function is to search._Off Index of the position at which the search is to begin._Ptr The C-string for which the member function is to search._Count The number of characters, counting forward from the first character, in the C-string for which the member function is to search._Str The string for which the member function is to search.3.实例解说:#include<string>#include<iostream>using namespace std;int main( ){string name("AnnaBelle");int pos1 = name.find("Anna");//int pos1 = name.find("Anna",2);//string::size_type pos1 = name.find("Anna",2);//basic_string <char>::size_type pos1 = name.find("Anna",2);cout<<pos1;}Msdn:实例2、Rfind 从末端起反向查找子串Msdn: rfind method1.参数定义size_type rfind(value_type _Ch,size_type _Off = npos //默认为npos=-1.赋值给unsigned,则为最大值。

) const;size_type rfind(const value_type* _Ptr,size_type _Off = npos) const;size_type rfind(const value_type* _Ptr,size_type _Off,size_type _Count) const;size_type rfind(const basic_string<CharType, Traits, Allocator>& _Str,size_type _Off = npos) const;2.实例解说#include<string>#include<iostream>int main( ){using namespace std;string text("Mississippi");string::size_type first_pos = text.find("is"); //return 1;string::size_type last_pos = text.rfind("is"); //return 4;cout<<first_pos<<endl<<last_pos;}3、Find_first_of 查找任意字符第一次出现的位置如果在查找字符串时希望匹配任意指定的字符。

看例子。

Msdn: find_first_of method1.参数定义size_type find_first_not_of(value_type _Ch,size_type _Off = 0) const;size_type find_first_of(const value_type* _Ptr,size_type _Off = 0) const;size_type find_first_of(const value_type* _Ptr,size_type _Off,size_type _Count) const;size_type find_first_of(const basic_string<CharType, Traits, Allocator>& _Str, size_type _Off = 0) const;2.实例:#include<string>#include<iostream>int main( ){using namespace std;string numerics("0123456789");string name("r2d2");string::size_type pos=name.find_first_of(numerics);cout<<"Found number at index:"<<pos<<endl;cout<<"Element is :"<<name[pos]<<endl;}4、find_first_not_of 查找不在参数里的字符,返回第一个位置Msdn: find_first_not_of除了寻找匹配的位置外,还可以调用find_first_not_of 函数查找第一个与实参不匹配的位置。

1.参数定义size_type find_first_not_of(value_type _Ch,size_type _Off = 0) const;size_type find_first_not_of(const value_type* _Ptr,size_type _Off = 0) const;size_type find_first_not_of(const value_type* _Ptr,size_type _Off,size_type _Count) const;size_type find_first_not_of(const basic_string<CharType, Traits, Allocator>& _Str,size_type _Off = 0) const;2.实例解说#include<string>#include<iostream>int main( ){using namespace std;string numerics("0123456789");string name("03714p3");string::size_type pos=name.find_first_not_of(numerics);cout<<"Found number at index:"<<pos<<endl;cout<<"Element is :"<<name[pos]<<endl;}5、find_last_of 查找任意字符的最后一次出现。

Msdn: find_last_of1.参数定义size_type find_last_of(value_type _Ch,size_type _Off = npos) const;size_type find_last_of(const value_type* _Ptr,size_type _Off = npos) const;size_type find_last_of(const value_type* _Ptr,size_type _Off,size_type _Count) const;size_type find_last_of(size_type _Off = npos) const;2.实例解说6、find_last_not_of 从末端起,反向查找不再参数里的字符。