#include<bits/stdc++.h>
using namespace std;
int main(){
//获取一行字符串
string s="What we have here is a failure to communicate";
// getline(cin,s);
//打印
cout<<s[0]<<endl;
//获取字符串的长度
cout<<"字符串的长度:"<<s.length()<<endl;
string sub = s.substr(21);
cout << "The original string is " << s << endl;
cout << "The substring is " << sub << endl;
//size()函数返回字符串中现在拥有的字符数。
cout<<s.size();
//resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。
s.resize(100,'x');
cout<<s<<endl;
//replace替换
s = "They say he carved it himself...from a BIGGER spoon";
string s2 = "find your soul-mate, Homer.";
s.replace( 32, s2.length(), s2 );
cout << s << endl;
//string的copy
char* c=new char[10];
strcpy(c,s.c_str());
cout<<c;
//添加文本
s.append(10,'*');
cout<<s<<endl;
//查找say的位置
unsigned int loc = s.find_last_of ( "say", s.length() );
if( loc != string::npos )
cout << "Found say at " << loc << endl;
else
cout << "Didn't find say" << endl;
//插入insert方法
//begin和end方法
return 0;
}
算法c++之stl---string的详细使用
最新推荐文章于 2024-05-16 18:52:44 发布