2011-10-06 94 views
0

我是_bstr_t的新手,仍然試圖弄清楚它。我試圖檢查一個特定的字符串x是否包含在bstring中的任何位置。我通常會喜歡的東西;如何檢查_bstr_t是否包含(類似於str.find)字符串

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

只是爲了記錄預期的平臺是windows。

回答

2

沒有必要使用_bstr_t。使用BSTR類型。

接下來,閱讀Eric's Complete Guide to BSTR Semantics

最後,您可以在本機代碼中使用BSTR,您將以普通字符數組的方式在大多數個案中使用。

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

MSDN for wcsstr

+0

感謝您的幫助,但是編譯器似乎不喜歡某事,'錯誤C2665:'wcsstr':2個重載都不能轉換所有的參數類型......'const wchar_t * wcsstr(const wchar_t * ,const wchar_t *)'...'wchar_t * wcsstr(wchar_t *,const wchar_t *)' – pondigi

+0

很難說沒有看到您的代碼。可能是一個'const'問題?您可以隨時使用新代碼提出新問題。 –

+0

無論如何感謝哥們,虐待檢查const – pondigi

1

你的例子似乎試圖從STL中使用string :: find。但是你需要指定「String」類型的變量(大寫)。如果你不是做的事:

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

您的例子編譯。你真的有一個BSTR或_bstr_t,你需要與你沒有顯示的工作?即使這樣,從_bstr_t開始生成一個std :: string也很容易,在這之後你可以像平常一樣使用STL。

相關問題