using boost string algorithm with MFC CString to check for the end of a string

这一生的挚爱 提交于 2019-12-11 03:59:22

问题


I need to check whether my CString object in MFC ends with a specific string.

I know that boost::algorithm has many functions meant for string manipulation and that in the header boost/algorithm/string/predicate.hpp could it be used for that purpose.

I usually use this library with std::string. Do you know a convenient way to use this library also with CString?

I know that the library is generic that can be used also with other string libraries used as template arguments, but it is not clear (and whether is possible) to apply this feature to CString.

Can you help me with that in case it is possible?


回答1:


According to Boost String Algorithms Library, "consult the design chapter to see precise specification of supported string types", which says amongst other things, "first requirement of string-type is that it must [be] accessible using Boost.Range", and note at the bottom the MFC/ATL implementation written by Shunsuke Sogame which should allow you to combine libraries.

Edit: Since you mention regex in the comments below, this is all you really need to do (assuming a unicode build):

CString inputString;
wcmatch matchGroups;
wregex yourRegex(L"^(.*)$"), regex::icase);
if (regex_search(static_cast<LPCWSTR>(inputString), matchGroups, yourRegex))
{
    CString firstCapture = matchGroups[1].str().c_str();
}

Note how we reduce the different string types to raw pointers to pass them between libraries. Replace my contrived yourRegex with your requirements, including whether or not you ignore case or are explicit about anchors.




回答2:


Why don't you save yourself the trouble and just use CStringT::Right?



来源:https://stackoverflow.com/questions/3733714/using-boost-string-algorithm-with-mfc-cstring-to-check-for-the-end-of-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!