我需要检查wstring是否以特定字符串开头。
const wstring str = "Hello World";
wstring temp="Hello ";
如何检查 str
是否以 temp
开头?
const wstring str = "Hello World";
wstring temp="Hello ";
如何检查 str
是否以 temp
开头?
首先使用宽字符字面量,然后就轻而易举了:
std::wstring const str = L"Hello World";
// one method:
if (str.substr(0, 6) == L"Hello ") { /* yay */ }
// another method, better:
if (str.find(L"Hello ") == 0) { /* hooray */ }
if (str.find(L"Hello ") == 0) { /* hooray */ }
吗?因为 OP 想知道字符串是否以 Hello 开头。 - Praetorian