string的构造函数
#include<iostream> #include<string> using namespace std; //string的构造函数 void test01() { string s1;//默认构造 const char* str = "hello world"; string s2(str);//把c_string转换成string cout << "s2= " << s2 << endl; string s3(s2);//拷贝构造函数 cout << "s3= " << s3 << endl; string s4(10, 'a'); cout << "s4= " << s4 << endl; } int main() { test01(); system("pause"); return 0; }
string的赋值操作
#include<iostream> #include<string> using namespace std; //sting赋值操作 void test01() { string str1; str1 = "hello world"; cout << "str1= " << str1 << endl; string str2; str2 = str1; cout << "str2= " << str2 << endl; string str3; str3 = 'a'; cout << "str3= " << str3 << endl; string str4; str4.assign("hello C++"); cout << "str4= " << str4 << endl; string str5; str5.assign("hello C++", 5);//输出字符串的前五个字符 cout << "str5= " << str5 << endl; string str6; str6.assign(str5); cout << "str6= " << str6 << endl; string str7; str7.assign(10, 'w'); cout << "str7= " << str7 << endl; } int main() { test01(); system("pause"); return 0; }
string的字符串拼接
#include<iostream> #include<string> using namespace std; //字符串拼接 void test01() { string str1 = "我"; str1 += "爱玩游戏"; cout << "str1= " << str1 << endl; str1 += ':'; cout << "str1= " << str1 << endl; string str2="LOL DNF"; str1 += str2; cout << "str1= " << str1 << endl; string str3 = "I "; str3.append("love "); cout << "str3= " << str3 << endl; str3.append("game abcde", 4); cout << "str3= " << str3 << endl; //str3.append(str2); //cout << "str3= " << str3 << endl; //str3.append(str2, 0, 3);//只截取LOL, str3.append(str2, 4, 7);//只截取DNF,参数2:从那个位置开始截取,参数3:截取字符的个数 cout << "str3= " << str3 << endl; } int main() { test01(); system("pause"); return 0; }
string的查找和替换
#include<iostream> #include<string> using namespace std; //字符串查找和替换 //查找 void test01() { //find string str1 = "abcdefgde"; int pos=str1.find("df"); if (pos == -1) { cout << "未找到字符串" << endl; } else { cout << "找到字符串: pos = " << pos << endl;//返回的是起始下标 } //rfind和find的区别 //rfind 从右往左查,find从左往右查 pos = str1.rfind("de"); cout << pos << endl; } //替换 void test02() { string str1 = "abcdefg"; //从1号位置起 3个字符替换成1111 str1.replace(1, 3, "1111"); cout << str1 << endl; } int main() { //test01(); test02(); system("pause"); return 0; }
string的字符串比较
#include<iostream> using namespace std; //字符串比较 void test01() { string str1 = "hello"; string str2 = "xello"; if (str1.compare(str2) == 0) { cout << "str1 等于 str2" << endl; } else if(str1.compare(str2) > 0) { cout << "str1 大于 str2" << endl; } else if (str1.compare(str2) < 0) { cout << "str1 小于 str2" << endl; } } int main() { test01(); system("pause"); return 0; }
string的字符存取
#include<iostream> #include<string> using namespace std; //字符存取 void test01() { string str = "hello"; cout << str << endl; //通过 [] 访问单个字符 for (int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; //通过at的方式访问单个字符 for (int i = 0; i < str.size(); i++) { cout << str.at(i) << " "; } cout << endl; //修改单个字符 str[0] = 'x'; cout << str << endl; } int main() { test01(); system("pause"); return 0; }
string的插入和删除
#include<iostream> #include<string> using namespace std; //字符串 插入和删除 void test01() { string str = "hello"; //插入 str.insert(1, "111");//从某个位置起,插入字符串 cout << str << endl;// 输出h111ello //删除 str.erase(1, 3); //从参数1起,删除长度为参数2的子串 cout << str << endl;// 输出hello } int main() { test01(); system("pause"); return 0; }
string的子串
#include<iostream> using namespace std; //string求子串 void test01() { string str = "abcdefg"; //从参数1 开始,截取长度为参数2的子串 string subStr = str.substr(1, 3); cout << subStr << endl;// 输出bcd } //实用操作 void test02() { string email = "zhangsan@sina.com"; //从邮箱地址中 获取用户信息 int pos = email.find("@"); cout << pos << endl; string userName = email.substr(0, pos); cout << userName << endl; } int main() { //test01(); test02(); system("pause"); return 0; }