繁体   English   中英

如何检查_bstr_t是否包含(类似于str.find)字符串

[英]How to check if a _bstr_t contains (similar to str.find) a string

我是_bstr_t的新手,但仍在尝试解决这个问题。 我正在尝试检查bstring中是否包含特定的字符串x 我通常会喜欢的东西;

String x = "hello";
String example = "You! hello there";
...
if (example.find(x) != string::npos) {
...

仅出于记录目的,预期的平台是Windows。

无需使用_bstr_t 使用BSTR类型。

接下来,阅读Eric的BSTR语义完整指南

最后,您可以像在大多数情况下使用普通字符数组一样,在本机代码中使用BSTR。

BSTR bstr = SysAllocString(L"FooBarBazQux");
if (wcsstr(bstr, L"Bar") != NULL) {
  // Found it! Do something.
} else {
  // Not there.
}
SysFreeString(bstr);

适用于wcsstr的MSDN

您的示例似乎正在尝试使用STL中的string :: find。 但是,您可以指定类型为“字符串”(大写)的变量。 如果您改为:

using namespace std;
string x = "hello";
string example = "You! hello there";
...

您的示例将编译。 您实际上有未显示的BSTR或_bstr_t吗? 即使是这样,从_bstr_t中创建std :: string也很容易,然后,您可以像通常那样使用STL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM