c++ wstring如何进行字符串查找
导读:在C++中,你可以使用std::wstring的find( 方法来查找子字符串 #include <iostream> #include <string> int main( { std::wstring...
在C++中,你可以使用std::wstring
的find()
方法来查找子字符串
#include <
iostream>
#include <
string>
int main() {
std::wstring str = L"这是一个示例字符串";
std::wstring to_find = L"示例";
size_t found = str.find(to_find);
if (found != std::wstring::npos) {
std::wcout <
<
L"找到子字符串: " <
<
to_find <
<
std::endl;
std::wcout <
<
L"位置: " <
<
found <
<
std::endl;
}
else {
std::wcout <
<
L"未找到子字符串" <
<
std::endl;
}
return 0;
}
在这个例子中,我们首先定义了一个宽字符串str
和一个要查找的子字符串to_find
。然后,我们使用find()
方法查找子字符串的位置。如果找到了子字符串,find()
方法返回子字符串的起始位置;否则,它返回std::wstring::npos
。最后,我们根据find()
方法的返回值输出相应的结果。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: c++ wstring如何进行字符串查找
本文地址: https://pptw.com/jishu/708828.html