#include <iostream>
#include <string>
using namespace std;
// 字符串的构造
void test01() {
//默认构造
string s1;
//使用char*字符串构造
const char *str = "hello world";
string s2(str);
cout << "s2=" << s2 << endl;
//使用string对象初始化
string s3(s2);
cout << "s3=" << s3 << endl;
//使用n个字符初始化
string s4(10, 'a');
cout << "s4=" << s4 << endl;
};
/**
* 赋值操作
* string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符值给当前的字符串
string& assign(const char *s);//把字符串s赋给当前的字符串
string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s);//把字符串s赋给当前字符串
string& assign(int n, char c);//用n个字符赋给当前字符串
*/
void test02() {
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, 'e');
cout << "str7 = " << str7 << endl;
};
//string字符串拼接
/**
*string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& operator+=(const string& str);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=(const string& str)
string& append(const string &s,int pos,int n); //字符s中从pos开始的n个字符连接到字符串结尾
*/
void test03() {
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.assign(" love ");
cout << "str3 = " << str3 << endl;
str3.append("game abcde", 5);
cout << "str3 = " << str3 << endl;
// str3.append(str2);
str3.append(str2, 0, 3);
cout << "str3 = " << str3 << endl;
};
//字符串查找
void test04() {
string str = "abcdefgde";
// 从左往右查
int pos = str.find("de");
cout << "pos = " << pos << endl;
//从右往左查
pos = str.rfind("de");
cout << "rfind pos = " << pos << endl;
};
//字符串替换
void test05() {
string str1 = "abcdefg";
//从1位置起2个字符,替换为"1231231"
str1.replace(1, 2, "1231231");
// str1=a1231231defg
cout << "str1=" << str1 << endl;
};
//字符串比较
// = 返回 0
// > 返回 1
//< 返回 -1
void test06() {
string str1 = "aello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "str1等于str2" << endl;
} else if (str1.compare(str2) > 0) {
cout << "str1大于str2" << endl;
} else {
cout << "str1小于str2" << endl;
}
};
//string 字符存取
void test07() {
string str = "hello";
// cout << "str=" << str << endl;
//1、通过[]访问单个字符
for (int i = 0; i < str.size(); ++i) {
cout << str[i] << " ";
}
cout << endl;
//2、通过at方法访问单个字符
for (int i = 0; i < str.size(); ++i) {
cout << str.at(i) << " ";
}
cout << endl;
//修改单个字符
str[0] = 'x';
str.at(1) = 'x';
// xxllo
cout << "str=" << str << endl;
}
/*
* string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
*/
//字符串的插入和删除
void test08() {
string str = "hello";
// 插入
str.insert(1, "111");
// h111ello
cout << str << endl;
//hello
str.erase(1, 3);
cout << str << endl;
}
//string子串
void test09() {
string str = "abcdef";
string subStr = str.substr(1, 3);
// subStr=bcd
cout << "subStr=" << subStr << endl;
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();
// test03();
// test04();
// test05();
// test06();
// test07();
// test08();
test09();
system("pause");
return 0;
}