标准库类型string表示可变长的字符序列,string本身是类,与容器 *** 作相似,使用string类型必须首先包含#include
1、string 构造函数
string s1; string默认构造
const char* str="hello world"; c风格字符串
string s2(str); 将char*字符串拷贝给s2
string s3(s2); 拷贝构造函数
string s4(n,'value'); 将n个value赋s4
#include#include using namespace std; int main() { string s1; const char* str = "hello world"; string s2(str); string s3(s2); string s4(10, 'a'); cout << "str : " << str << endl; cout << "s2 : " << s2 << endl; cout << "s3 : " << s3 << endl; cout << "s4 : " << s4 << endl; system("pause"); return 0; }
、
2、string 容器赋值 *** 作
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个字符串c赋给当前字符串
#include#include using namespace std; int main() { string s1; s1 = "hello world"; string s2; s2 = s1; string s3; s3 = 'c'; string s4; s4.assign("hello world"); string s5; s5.assign("hello world", 5); string s6; s6.assign(s2); string s7; s7.assign(5, 'c'); cout << "s1 : " << s1 << endl; cout << "s2 : " << s2 << endl; cout << "s3 : " << s3 << endl; cout << "s4 : " << s4 << endl; cout << "s5 : " << s5 << endl; cout << "s6 : " << s6 << endl; cout << "s7 : " << s7 << endl; system("pause"); return 0; }
4、string 容器插入和删除
insert(pos,"value"); 在pos位置插入value值
erase(beg,end); 清除[beg,end]区间的字符,若只给beg一个值默认从beg开始全部清除;
clear(); 清空容器
#include#include using namespace std; int main() { string s1= "hello world"; cout << "s1 : " << s1 << endl; s1.insert(3, "value"); cout << "插入后s1 : " << s1 << endl; s1.erase(2, 4); cout << "指定区间s1 : " << s1 << endl; s1.erase(s1.begin()+2,s1.end()-4); cout << "利用迭代器指定区间s1 : " << s1 << endl; s1.erase(2); cout << "只给beg值s1 : " << s1 << endl; s1.clear(); cout << "请空s1 : " << s1 << endl; system("pause"); return 0; }
5、string容器拼接
string& operator+=(const char*str); 重载+= *** 作符
string& operator+=(const char c);
string& operator+=(const string& str);
append(const char* s); 把字符串s连接到当前字符串尾部
append(const char*s,int n); 把字符串前n个字符连接到当前字符串尾部
append(const string &s); 把字符串s连接到当前字符串尾部
append(const string &s,int pos,int n); 把字符串s从pos开始n个字符连接到当前字符串尾部
#include#include using namespace std; int main() { string s1= "hello "; s1 += "world"; cout << "s1 : " << s1 << endl; s1 += '!'; cout << "s1 : " << s1 << endl; string s2="!6!6!6!"; s1 += s2; cout << "s1 : " << s1 << endl; s1.append("yes"); cout << "s1 : " << s1 << endl; s1.append("hhhhhhhhh", 2); cout << "s1 : " << s1 << endl; s1.append(s2); cout << "s1 : " << s1 << endl; s1.append(s2, 2, 2); system("pause"); return 0; }
6、字符串查找和替换
find(); 从左至右
rfind(); 从右至左
replace(n,m,"字符串“); 从n号位置起m个字符替换成字符串
#include#include using namespace std; int main() { string s1= "hello world"; char c = s1.find(2); cout << "c : " << c << endl; char b = s1.rfind(2); cout << "b : " << b << endl; s1.replace(2, 2, "value"); cout << "s1 : " << s1 << endl; system("pause"); return 0; }
7、字符串比较
str1.compare(str2); 返回值 0相等,-1小于 ,1大于
#include#include using namespace std; int main() { string s1= "hello world"; string s2 = "helle w"; int a; a = s1.compare(s2); cout << "a = " << a << endl; system("pause"); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)