스트링 마지막에 일치 스트링 찾기(find string)

Posted by [하늘이]
2018. 12. 5. 21:29 IT/C, C++
반응형

ref web : http://thispointer.com/c-how-to-check-if-a-string-ends-with-an-another-given-string/


/* Case Sensitive Implementation of endsWith()

* It checks if the string 'mainStr' ends with given string 'toMatch'

* /

bool endsWith(const std::string &mainStr, const std::string &toMatch)

{

 if(mainStr.size() >= toMatch.size() &&

     mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)

     return true;

   else

     return false;

}

                      또는

/*

 * Case Sensitive Implementation of endsWith()

 * It checks if the string 'mainStr' ends with given string 'toMatch'

 * /

bool endsWith_secApproach(const std::string &mainStr, const std::string &toMatch)

{

       auto it = toMatch.begin();

               return mainStr.size() >= toMatch.size() &&

                               std::all_of(std::next(mainStr.begin(),mainStr.size() - toMatch.size()), mainStr.end(), [&it](const char & c){

                                       return c == *(it++)  ; //*

               } );

}

반응형