#include
#include
using namespace std;
//string包含在命名空间std中
int main()
{
//初始化string
string str=“sasdass”;
string str2(“ss”);
string str3=str1;
//遍历字符串
for(int i=0;i<str.length;i++)
{
cout<<str[i]<<str.at(i);
//str[i]和str.at(i)的区别在于at会抛出异常,你可以使用catch捕捉
}
//迭代器方式遍历
for (string::iterator it = str.begin(); it != str.end(); it++)
{
cout<<str[i];
}
//string 与const char*转换
const char* p=str.c_str();//s>c
string str4=p;//c>s
//拷贝字符到字符数组中
char ch[10];
str.copy(ch,4,0);//这个函数意思是从字符串的-位置开始拷贝4个字符带str
//字符串连接
str2=str+str2;//或者str2.append(str);
//字符串查找
str.find("ss",0);//在str字符串中查找第一个ss字符串
//替换
str.replace(0,3,"ss");//从0位置开始删除接着的三个字符,并改成"ss"
//插入
str.insert(0,"sss");//在0位置插入字符串sss
//删除
str.erase(0,1);//从0位置开始删除2字符
}