赋值操作:=,assign()
追加,插入:append(), push_back(), insert()
大小:size(), length(), capacity()
位置访问:[ ], at()
擦除字符:erase()
替换:replace()
比较:compare()
交换:swap()
子串:substr()
判断是否为空:empty()
删除全部字符:clear()
以C string形式返回:c_str()
以字符数组形式返回: data()
查找字符或字符串:find(), find_first_of(), find_last_of(),
反向查找:rfind()
代码一:
#include "stdafx.h"
#include "iostream"
#include <string>
using namespace std;
int main()
{
string s; // 默认构造函数,s为空串
s.assign("my book");//等同于=,将"my book"赋值给s
cout<<"s.assign()="<<s<<endl;
// push_back 追加单个字符
s.push_back('AB');
cout<<"s.push_back()="<<s<<endl;
s.append("!!"); //追加,字符串之后追加
cout<<"s.append()="<<s<<endl;
// 字符串现有的字符数,size()和length()等效
cout<<"The size of s "<<s<<" is "<<s.size()<<endl; //int size()
cout<<"The length of s "<<s<<" is "<<s.length()<<endl; //int length()
cout<<"The capacity of s "<<s<<" is "<<s.capacity()<<endl; //int capacity()
// max_size() 当前C++字符串最多能包含的字符数
cout<<"The max size of s "<<s<<" is "<<s.max_size()<<endl; //int max_size()
// 下表操作符[]和at()对所在位置的字符进行访问,at()会检查索引是否有效(0-length())
cout<<"s.at(3)="<<s.at(3)<<endl;
cout<<"s[3]="<<s[3]<<endl;
cout<<"s="<<s<<endl;
cout<<"string s after erase(5) is "<<s.erase(5)<<endl; //从索引5开始往后全删除
s.assign("The cup is very beautiful!");
cout<<"s="<<s<<endl;
cout<<"string s replace(2,3,\"good\") is "<<s.replace(2,3,"good")<<endl;//从索引2开始的3个替换为“good”
cout<<"string s after erase(7,5) is "<<s.erase(7,5)<<endl; //从索引7开始往后删5个
s.insert(3,"lovely"); //在位置3处插入逗号后的字符串
cout<<"s.insert()="<<s<<endl;
system("pause");
return 0;
}
输出结果:
代码二:
#include "stdafx.h"
#include "iostream"
#include <string>
using namespace std;
int main()
{
string str="my book!";
string::size_type pos;
pos=str.find("book"); // find函数返回字符串“book”在str中第一次出现的位置(下标)
if(pos!=str.npos) // 如果没找到“book”,返回npos,一个很大的值
{
cout<<"book 出现的下标是:"<<pos<<endl;
}
str.assign("welcome to china.");
pos=str.find("to",6); // 从str下标6‘e’开始,查找字符串“to”,返回to在str中的下标
cout<<"str.find(to,6) is :"<<pos<<endl;
pos=str.rfind("to");// 反向查找
cout<<"str.rfind(to) is :"<<pos<<endl;
string substr="o"; // 定义字串为"o"
cout<<"str.find_first_of(substr):"<<str.find_first_of(substr)<<endl;
cout<<"str.find_last_of(substr):"<<str.find_last_of(substr)<<endl;
system("pause");
return 0;
}
输出结果:
代码3:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1; //默认构造函数,创建一个长度为0的字符串
string str2("hello"); //直接初始化 "hello"
string str3 = "world"; //赋值初始化 "world"
string str4(str2); //拷贝构造函数 "hello"
string str5(str3,1,4); //从字符串str3的位置1(第2个字符)开始取4个字符,用来初始化str5 "orld"
string str6(str2,2); //用指针str3指向的字符串中的第2个字符开始初始化string对象str6 “llo”
string str7(8,'s'); //用8个重复的字符's'来初始化string类的对象str7 "ssssssss"
cout<<"str1 = "<<str1<<endl;
cout<<"str2 = "<<str2<<endl; //"hello"
cout<<"str3 = "<<str3<<endl; //"world"
cout<<"str4 = "<<str4<<endl; //"hello"
cout<<"str5 = "<<str5<<endl; //"orld"
cout<<"str6 = "<<str6<<endl; //“llo”
cout<<"str7 = "<<str7<<endl; //"ssssssss"
cout<<endl;
str1.append("my cup");//将字符串"my cup"追加到字符串str1之后
str2.assign("good luck");//将“good luck”赋值给str2
cout<<"str11 = "<<str1<<endl; //"my cup"
cout<<"str21 = "<<str2<<endl; //“good luck”
str1.insert(3,"green "); //将“green”插入到str1的第3个位置(下标)
cout<<"str12 = "<<str1<<endl; //"my green cup"
string str22 = str2.substr(0,4);//从字符串str2的第0个位置取4个字符 "good"
cout<<"str22 = "<<str22<<endl;
int pos = str1.find("cup"); //在字符串str1中查找“cup”并返回第一次出现的位置
cout<<"pos = "<<pos<<endl;
str3.swap(str7); //交换str3和str7的内容
if(str1.compare(str2)==0)
{
cout<<"str1 is equal to str2."<<endl;
}
else if(str1.compare(str2)>0)
{
cout<<"str1 is bigger than str2."<<endl;
}
str2+=str1;
cout<<"str23 = "<<str2<<endl;
system("pause");
return 0;
}
输出结果: