windows 如何检查 _bstr_t 是否包含(类似于 str.find)一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7676864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a _bstr_t contains (similar to str.find) a string
提问by pondigi
I am new to _bstr_t
's and still trying to get the hang of it. I was trying to check whether a particular string x
is contained anywhere within the bstring. Something I would normally do like;
我是_bstr_t
's 的新手,仍在努力掌握它。我试图检查特定字符串x
是否包含在 bstring 中的任何位置。我通常会喜欢做的事情;
String x = "hello";
String example = "You! hello there";
...
if (example.find(x) != string::npos) {
...
Just for the record the intended platform is windows.
只是为了记录,预期的平台是windows。
回答by i_am_jorf
There is no need to use _bstr_t
. Use the BSTR
type.
没有必要使用_bstr_t
. 使用BSTR
类型。
Next, read Eric's Complete Guide to BSTR Semantics.
接下来,阅读Eric 的 BSTR 语义完整指南。
Lastly, you can use the BSTR in native code the way you would a normal character array in mostcases.
最后,在大多数情况下,您可以像使用普通字符数组一样在本机代码中使用 BSTR 。
BSTR bstr = SysAllocString(L"FooBarBazQux");
if (wcsstr(bstr, L"Bar") != NULL) {
// Found it! Do something.
} else {
// Not there.
}
SysFreeString(bstr);
回答by David Grigsby
Your example appears to be trying to use string::find from STL. But you specify your variables of type "String" (capitalized). If you instead did:
您的示例似乎试图使用 STL 中的 string::find。但是您指定了“字符串”类型的变量(大写)。如果你这样做:
using namespace std;
string x = "hello";
string example = "You! hello there";
...
your example would compile. Do you actually have a BSTR or _bstr_t that you need to work with that you haven't shown? Even if so, it's pretty easy to make an std::string from a _bstr_t, and after that you can use STL as you normally would.
你的例子会编译。你真的有一个你没有展示的 BSTR 或 _bstr_t 需要使用吗?即使是这样,从 _bstr_t 生成 std::string 也很容易,之后您可以像往常一样使用 STL。