c++ string startsWith endswith

1736 ワード

シミュレーションしてみよう
#include
#include
using namespace std;

int startsWith(string s, string sub){
        return s.find(sub)==0?1:0;
}

int endsWith(string s,string sub){
        return s.rfind(sub)==(s.length()-sub.length())?1:0;
}

int main(){
        string str = "helloWorld";
        string preStr = "he";
        string sufStr = "rld";
        if(startsWith(str,preStr)){
                cout<" starts with "+preStr<if(endsWith(str,sufStr)){
                cout<" ends with "+sufStr<return 0;
}