Is there a reverse function for strstr

后端 未结 17 2327
闹比i
闹比i 2020-12-18 18:28

I am trying to find a similar function to strstr that searches a substring starting from the end towards the beginning of the string.

17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 19:24

    If you can use C++, you can search strings like this:

    std::string::iterator found=std::search(haystack.rbegin(), haystack.rend(), needle.rbegin(), needle.rend()).base();
    // => yields haystack.begin() if not found, otherwise, an iterator past-the end of the occurence of needle
    

提交回复
热议问题