4

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?

Community
  • 1
  • 1
JChan
  • 1,411
  • 4
  • 24
  • 34
  • sounds a lot like http://stackoverflow.com/questions/1878001/how-do-i-check-if-a-c-string-starts-with-a-certain-string-and-convert-a-sub – celavek Jul 24 '12 at 22:34

1 Answers1

11

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 */ }
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084