c++ string操作
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void test01()
{
string str1;
str1="hello world";
cout<<str1<<endl;
string str2;
str2=str1;
cout<<str2<<endl;
string str3;
str3='a';
cout<<str3<<endl;
string str4;
str4.assign("hello C++");
cout<<str4<<endl;
string str5;
str5.assign("hello C++",5);
cout<<str5<<endl;
string str6;
str6.assign(str5);
cout<<str6<<endl;
string str7;
str7.assign(10,'w');
cout<<str7<<endl;
}
void test02()
{
string str1="我";
str1+="爱玩游戏";
cout<<"str1="<<str1<<endl;
str1+=':';
cout<<"str1="<<str1<<endl;
string str2="LOL DNF";
str1+=str2;
cout<<str1<<endl;
string str3="I";
str3.append(" love");
cout<<"str3="<<str3<<endl;
str3.append(" game abcde",5);
cout<<"str3="<<str3<<endl;
str3.append(str2);
cout<<"str3="<<str3<<endl;
str3.append(str3,11,3);
cout<<"str3="<<str3<<endl;
}
void test03()
{
string str1="hello world";
cout<<str1.find('l')<<endl;
cout<<str1.rfind('l')<<endl;
string str2="C++";
str1.replace(6,5,str2);
cout<<"str1="<<str1<<endl;
}
void test04()
{
string str1="hello world";
string str2="hello C++";
if (str1.compare(str2)==0)
{
cout<<"str1等于str2"<<endl;
} else if (str1.compare(str2)>0)
{
cout<<"str1大于str2"<<endl;
}
else{
cout<<"str1小于str2"<<endl;
}
}
int main() {
test01();
test02();
test03();
test04();
return 0;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-10966.html