Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if <wstring> starts with a certain string using C++ [duplicate]

Tags:

c++

visual-c++

Possible Duplicate:
how to check string start in C++

I need to check if wstring begins with a particular string.

const wstring str = "Hello World";
wstring temp="Hello ";

How I can check str begins with temp or not?

like image 233
JChan Avatar asked Dec 20 '22 18:12

JChan


1 Answers

Use wide literals for starters; then it's a breeze:

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 */ }
like image 61
Kerrek SB Avatar answered Dec 24 '22 01:12

Kerrek SB