string中常见的成员函数
示例代码:
string s= string("abcdefg");char ch[] = "abcdefgd";//调用构造函数全部复制string str1 = string(s);//构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str2string str2 = string(s,2,5);//复制ch前5个字符串string str3 = string(ch,5);//将5个'6'赋给字符串string str4 = string(5,'6');//string str5 = string(s.begin(),s.end());cout << "oringin s = " + s << endl;cout << "oringin ch = " <<< endl;cout << "str1 = " + str1<< endl;cout << "str2 = " + str2< << "str3 = " + str3< << "str4 = " + str4< << "str5 = " + str5<
输出结果:
string的常用方法
取string中元素
使用at()或[]去字符串的元素
string s = "hello";char c = s.at(2);char ch = s[2];
上面两行的效果是一样的,都是将l取出。
字符串比较
compare函数
函数原型如下所示:int compare (const basic_string& s) const;
int compare (const Ch* p) const; int compare (size_type pos, size_type n, const basic_string& s) const; int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const; int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;
函数返回值介绍:若参与比较的两个串值相同,则函数返回 0;若字符串 S 按字典顺序要先于 S2,则返回负值;反之,则返回正值。下面举例说明如何使用 string 类的 compare() 函数。
具体示例如下:
string s1 = "Hello";string s2 = "Ok";string s3 = "abdcds";string s4("abdcds");cout << "s1 = " + s1 <<< "s2 = " + s2 < << "s3 = " + s3 < << "s4 = " + s4 < <<"s1.comapre(s2) = " << s1.compare(s2) < <<"s2.comapre(s3) = " << s2.compare(s3) < <<"s3.comapre(s4) = " << s3.compare(s4) <
输出结果如下:
s1 = Hellos2 = Oks3 = abdcdss4 = abdcdss1.comapre(s2) = -1s2.comapre(s3) = -1s3.comapre(s4) = 0
- 比较运算符 String 类的常见运算符包括 >、<、==、>=、<=、!=。其意义分别为"大于"、"小于"、"等于"、"大于等于"、"小于等于"、"不等于"
示例用法:
#include#include using namespace std;void TrueOrFalse (int x){cout << (x?"True":"False")< << "S1 <= CP2 returned ";TrueOrFalse (S1 <= CP2);cout << "S1 <= CP3 returned ";TrueOrFalse (S1 <= CP3);cout << "CP1 <= S1 returned ";TrueOrFalse (CP1 <= S1);cout << "CP2 <= S1 returned ";TrueOrFalse (CP2 <= S1);cout << "CP4 <= S1 returned ";TrueOrFalse (CP4 <= S1);cin.get();return 0;}
输出结果:
S1= DEFCP1 = ABCCP2 = DEFCP3 = DEFGCP4 = defS1 <= CP1 returned FalseS1 <= CP2 returned TrueS1 <= CP3 returned TrueCP1 <= S1 returned TrueCP2 <= S1 returned TrueCP4 <= S1 returned False
字符串内容的修改
使用append()函数
basic_string& append (const E * s); //在原始字符串后面追加字符串s
basic_string& append (const E * s, size_type n);//在原始字符串后面追加字符串 s 的前 n 个字符 basic_string& append (const basic_string& str, size_type pos,size_type n);//在原始字符串后面追加字符串 s 的子串 s [ pos,…,pos +n -1] basic_string& append (const basic_string& str); basic_string& append (size_type n, E c); //追加 n 个重复字符 basic_string& append (const_iterator first, const_iterator last); //使用迭代器追加
示例代码如下:
string s1 = "123456";string s2 = "abcdefgh";string str;str.assign(s1);cout <<"after use assign: str = " << str << endl;
使用insert函数
basic_string& insert (size_type p0 , const E * s); //插人 1 个字符至字符串 s 前面
basic_string& insert (size_type p0 , const E * s, size_type n); // 将 s 的前 3 个字符插入p0 位置 basic_string& insert (size_type p0, const basic_string& str); basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //选取 str 的子串 basic_string& insert (size_type p0, size_type n, E c); //在下标 p0 位置插入 n 个字符 c iterator insert (iterator it, E c); //在 it 位置插入字符 c void insert (iterator it, const_iterator first, const_iterator last); //在字符串前插入字符 void insert (iterator it, size_type n, E c) ; //在 it 位置重复插入 n 个字符 c
示例代码如下:
string A("ello");string B ;B.insert(1,A);cout << B << endl;A = "ello";B = "H";B.insert (1,"yanchy ",3);cout<< B <
字符串替换函数
方法原型:
basic_string& replace (size_type p0, size_type n0, const E * s); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符basic_string& replace (size_type p0, size_type n0, const basic_string& str); //使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); //使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开 始替换,替换字符 n0 个basic_string& replace (size_type p0, size_type n0, size_type n, E c); //使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符basic_string& replace (iterator first0, iterator last0, const E * s);//使用迭代器替换,和 1) 用法类似basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);//和 2) 类似basic_string& replace (iterator first0, iterator last0, const basic_string& str); //和 3) 类似basic_string& replace (iterator first0, iterator last0, size_type n, E c); //和 5) 类似basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last); //使用迭代器替换
示例代码如下:
#include#include using namespace std;int main (){string var ("abcdefghijklmn");const string dest ("1234");string dest2 ("567891234");var.replace (3,3, dest);cout << "1: " << var << endl;var = "abcdefghijklmn";var.replace (3,1, dest.c_str(), 1, 3);cout << "2: " << var << endl;var ="abcdefghijklmn";var.replace (3, 1, 5, 'x');cout << "3: " << var << endl;string::iterator itA, itB;string::iterator itC, itD;itA = var.begin();itB = var.end();var = "abcdefghijklmn";var.replace (itA, itB, dest);cout << "4: " << var << endl;itA = var.begin ();itB = var.end();itC = dest2.begin () +1;itD = dest2.end ();var = "abodefghijklmn";var.replace (itA, itB, itC, itD);cout << "5: " << var << endl;var = "abcdefghijklmn";var.replace (3, 1, dest.c_str(), 4); //这种方式会限定字符串替换的最大长度cout <<"6: " << var << endl;return 0;}
输出结果 :
1: abc1234ghijklmn2: abc234efghijklmn3: abcxxxxxefghijklmn4: 12345: 67891234efghijklmn6: abc1234efghijklmn