C++20提供了starts_with用于检查字符串是否以某个字符串开始,ends_with用于检查是否以某个字符串结束:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello and 88";
cout<<str.starts_with("hello")<<endl;
cout<<str.starts_with("h")<<endl;
cout<<str.starts_with("H")<<endl;
cout<<str.ends_with("8")<<endl;
cout<<str.ends_with("88")<<endl;
cout<<str.ends_with(" 88")<<endl;
return 0;
}
运行程序输出:
1
1
0
1
1
1