compare()
比较两个字符串
#include <string>
#include <iostream>
using namespace std;
int main(){
string{""};//string
string("abc");//char *
int ret=string{"a"}.compare(string{"b"});//大于1,小于-1,等于0
int ret2=string{"abc"}.compare(1,2,string{"bc"});//"abc"的[1,1+2)和”bc“比较
int ret3=string{"abc"}.compare(0,1,string{"Abc"},1,1);//a<b
return 0;
}
starts_with()
C++20
检查string是否始于给定前缀
#include <iostream>
#include <string>
using namespace std;
int main(){
string s{"good hello,world"};
if(s.starts_with('g'))
cout << "success" << endl;;
else
cout << "defeat" << endl;
s.starts_with("good");
return 0;
}
ends_with()
c++20
检查string是否终于给定后缀
#include <iostream>
#include <string>
using namespace std;
int main(){
string s{"good hello,world"};
if(s.ends_with('d'))
cout << "success" << endl;;
else
cout << "defeat" << endl;
s.end_with("world");
return 0;
}