views:

69

answers:

2

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?

A: 

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

Nikola Smiljanić
:)That's True! :)
Abruzzo Forte e Gentile
+1  A: 

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.

Sean Fausett
Thanks Sean! I will have a look at it and exploit it. Just to share right now I am using boost::regex with MFC String. They specialized it for MFC String and I am having a look at <boost/regex/mfc.hpp>
Abruzzo Forte e Gentile
Really, you just need to exploit that all string containers can be reduced to a raw pointer, so in this case pass the CString directly but use a `static_cast<LPCTSTR>()` on it first.
Sean Fausett