string 类提供字符串处理函数,利用这些函数,可以在字符串内查找字符,提取连续字符序列(称为子串),以及在字符串中删除和添加。
1.函数find_first_of()和 find_last_of() 执行简单的模式匹配,如在字符串中查找单个字符c。 函数find_first_of() 查找在字符串中第1个出现的字符c, 而函数find_last_of()查找最后一个出现的c。 匹配的位置是返回值。如果没有匹配发生,则函数返回-1.
int find_first_of(char c, int start = 0): 查找字符串中第1个出现的c,由位置start开始。 如果有匹配,则返回匹配位置;否则,返回-1.默认情况下,start为0,函数搜索整个字符串。
int find_last_of(char c): 查找字符串中最后一个出现的c。有匹配,则返回匹配位置;否则返回-1.该搜索在字符末尾查找匹配,所以没有提供起始位置。
string str = "Mississippi"; int index; // 's ' 在index 为 2、3、5、6处出现 index = str.find_first_of('s',0); // index为 2 index = str.find_first_of('s',4); // index为 5 index = str.find_first_of('s',7); // index为 -1 // ‘s’的最后出现在 index= 6 index = str.find_last_of('s'); // while 循环输出每个'i'的index while((index = str.find_first_of('i', index))!= -1) { cout << "index" << index << " "; index++; // restart search at next indx }输出结果: index 1 index 4 index 7 index 10
2.字符串中提取连续字符序列,既子串。 string substr(int start=0,int count= -1); 从起始位置开始复制字符串中的count 个字符,并返回这些字符作为子串。 如果字符串尾部小于count字符或者count 为-1,则字符串尾停止复制。 如果不使用参数调用只包括位置start,则substr()返回从位置开始到字符串尾部的子串。
#include<string> #include<iostream> using namespace std; main() { string s("12345asdf"); string a=s.substr(0,5); //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾 cout<<a<<endl; }输出结果为: 12345 find()函数在字符串中查找指定模式。该函数将字符串s和位置start作为参数,并查找 s的匹配作为子串。
int find(const string& s,int start = 0): 该搜索获得字符串s和位置start,并查找s的匹配作为子串。 如果有匹配,则返回匹配的位置;否则返回-1。默认情况下, start为0,函数搜索整个字符串。
string fullname = "Mark Tompkin", firstname, lastname; int index; index = str.find_last_of(' '); // index is 4 // firstname = "Mark" lastname = "Tompkin" firstname = fullname.sub string(0,index); lastname = fullname.substring(index+1); index = fullname.find("kin"); // 在 index = 9 匹配 "Kin" index = fullname.find("omp",0); // 在 index = 6 匹配 "omp" index = fullname.find("omp",7); // index is -1 (无匹配)3.添加和删除字符串
字符连接(+、+=)是在字符串尾添加字符串。insert()函数扩展了这个能力,允许在任意位置添加字符串。为了从字符串。为了从字符串中删除字符串,函数erase()可以从指定的位置开始删除字符。void insert(int statr,const string& s): 将子串s放入字符串中,起始于位置start。插入操作增加了原始字符串的长度。
void erase(int start=0,int count=-1): 从start开始,从字符串中删除count个字符。如果现有的字符串少于count个字符,或者count为-1,则删除到字符串尾部的所有字符。默认情况下,start为0,函数从字符串是起始位置开始删除字符串。默认情况下,函数也删除到字符串尾。 需要注意的是,不使用参数调用erase()函数时,将把字符串截断为长度为0的空字符串。
string str = "endfile"; string s = "string object type"; str += " mark"; str.inset(3, "-of-"); // str 是 "end-of-file mark" s.erase(7,7); // s 是 "string type" // 从index 为3处删除4个字符 s.erase(3,4); cout << s; // 输出:"strtype"4.c_str()返回C语言风格字符串的地址。 将字符串对象转换为c语言风格字符串。 char *c_str(); 返回一个等价于字符串对象的c语言风格字符串的地址。返回类型char*表示c语言风格字符串第1个字符的地址。
string filename = "input.dat"; // open 要求文件名是c语言风格的字符串 fin.open(filename.c_str());CString::Left(intnCount) ——返回字符串前nCount个字符的字符串 example: CString str(_T(“Shop,车间”)); str = str.Left(4); 结果:str=”Shop”;
CString::Right(int nCount) ——返回字符串后nCount个字符的字符串 example: CString str(_T(“Shop,车间”)); str = str.Right(2); 结果:str=”车间”;
CString Mid( int nFirst, int nCount ) const; //从左边第 nFirst+1 个字符开始,获取后面 nCount 个字符 CString Mid( int nFirst ) const; //从左边第 nCount+1 个字符开始,获取后面所有的字符
CString::Find(_T(“,”)) 返回“,”在字符串中的索引值
example: CString str(_T(“Shop,车间”)); int idex = str.Find(_T(“,”)); 此时:idex=4;
要想获得“,”右侧内容 str = str.Right(str.GetLength()-1-str.Find(_T(“,”))); 其中: str.GetLength()=7; -1排除“,” -str.Find(_T(“,”))排除“,”前的所有字
CString::ReverseFind int ReverseFind( TCHAR ch ) const; 返回值: 返回此CString对象中与要求的字符匹配的最后一个字符的索引;如果没有找到需要的字符则返回-1。 参数: ch 要搜索的字符。 说明:此成员函数在此CString对象中搜索与一个子串匹配的最后一个字符。此函数类似于运行时函数strrchr。 示例:// CString::ReverseFind示例: CString s( “abcabc” ); ASSERT( s.ReverseFind( ‘b’ ) == 4 );
