开发者

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

开发者 https://www.devze.com 2023-01-17 00:50 出处:网络
I need to check w开发者_如何学编程hether my CString object in MFC ends with a specific string.

I need to check w开发者_如何学编程hether 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消