How to check if string ends with .txt

后端 未结 11 1805
耶瑟儿~
耶瑟儿~ 2020-12-15 04:23

I am learning basic C++, and right now I have gotten a string from a user and I want to check if they typed the entire file name (including .txt) or not. I have the string,

11条回答
  •  不知归路
    2020-12-15 05:12

    2 options I can think of beside mentioned ones:
    1) regex - prob overkill for this, but simple regexes are nice and readable IMHO
    2) rbegin - kind of nice, could be I am missing something, but here it is:

    bool ends_with(const string& s, const string& ending)
    {
    return (s.size()>=ending.size()) && equal(ending.rbegin(), ending.rend(), s.rbegin());
    }
    

    http://coliru.stacked-crooked.com/a/4de3eafed3bff6e3

提交回复
热议问题