#include<iostream>#include<string>//在这个头文件中usingnamespace std;intmain(){//3种初始化的方法
string myStr ="Guangdong University of Technology";//结尾没有'\0'
string myStrCopy = myStr;
string tmpStr(5,'s');//tmpStr: "sssss"//获取字符串长度int len = myStr.length();//转换C风格字符串char c_myStr[35];strcpy_s(c_myStr, myStr.c_str());
cout << c_myStr << endl;//字符串拼接,printStr:"My school is Guangdong University of Technology"
string mySentence ="My school is";
string printStr = mySentence + myStr;//插入、删除字符串, "My school is "字符串长度为13
myStr.insert(0,"My school is ");//在下标0处插入字符串
myStr.erase(14);//删除下标13后面的所有字符
myStr.erase(2,14);//删除下标1后面的14个字符//此时myStr:"My", 重置myStr
myStr = myStrCopy;//提取子字符串
tmpStr = myStr.substr(0,9);//截取[0, 0+9]区间的字符串,9指的是截取的长度, tmpStr:"Guangdong"//字符串查找int index;
index = myStr.find("University",5);//从下标5开始查找
index = myStr.find("University");//从头开始找
index = myStr.rfind("University",30);//在下标30之前查找
index = myStr.rfind("University");//到结尾之前查找
index = myStr.find_first_of("d");//首次出现“d”的下标return0;}