1.关于带空格字符串函数的输入
a.getline(cin,str);
b.getline(cin,str,c);(c为字符。)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str1,str2;
getline(cin,str1); //输入带空格的字符串,当输入空格的时候终止
getline(cin,str2,'D'); //输入带空格的字符串,当输入‘D’的时候终止
cout<<str1<<endl;
cout<<str2<<endl;
}
运行结果:
2.关于字符串的截取
截取第a到第b个的字符串。
str.substr(a,b); substr从英文层面上很好理解,也就是子串呗。
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str.substr(1,5)<<endl;
}
运行结果:
3.一些字符串的处理
a.字符查找:
str.find©;
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str.find('w')<<endl;
}
运行结果:
b.字符串查找
str.find(str1);(str1为你想要查找的字符串);
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin>>str;
//查找一个字符串,如果找到,输出他的下标,如果没找到,输出负一
long long int index=str.find("world");
if(index<str.length())
{
cout<<index<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
运行结果:
c.字符串翻转输出:
reverse(str.begin(),str.end());
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str="helloworld";
reverse(str.begin(),str.end());
cout<<str<<endl;
}
运行结果:
有我在,天下无不可之事。