使用 MFC CString 的 boost 字符串算法来检查字符串的结尾

发布于 2024-09-24 07:06:52 字数 348 浏览 6 评论 0原文

我需要检查 MFC 中的 CString 对象是否以特定字符串结尾。

我知道 boost::algorithm 有许多用于字符串操作的函数,并且在 header boost/algorithm/string/predicate.hpp 中可以将其用于此目的。

我通常将此库与 std::string 一起使用。您知道与 CString 一起使用该库的便捷方法吗?

我知道该库是通用的,也可以与用作模板参数的其他字符串库一起使用,但尚不清楚(以及是否可能)将此功能应用于 CString。

如果可能的话,你能帮我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-10-01 07:06:52

根据 Boost 字符串算法库,“查阅设计章节查看支持的精确规范字符串类型”,除其他外,“字符串类型的首要要求是它必须可以使用 Boost.Range",并注意底部的 MFC/ATL 由 Shunsuke Sogame 编写,它应该允许您组合库。

编辑:由于您在下面的评论中提到了正则表达式,这就是您真正需要做的所有事情(假设是 unicode 构建):

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();
}

请注意我们如何将不同的字符串类型简化为原始指针以在库之间传递它们。将我设计的 yourRegex 替换为您的要求,包括您是否忽略大小写或是否明确锚点。

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.

南七夏 2024-10-01 07:06:52

为什么不给自己省点麻烦,直接使用 CStringT::Right?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文