How can I compare a wstring, such as L"Hello", to a string? If I need to have the same type, how can I convert them into the same type?
How can I compare a wstring , such as LHello , to a string
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you asked, here’s my standard conversion functions from string to wide string, implemented using C++
std::stringandstd::wstringclasses.First off, make sure to start your program with
set_locale:Now for the functions. First off, getting a wide string from a narrow string:
And going back, making a narrow string from a wide string. I call the narrow string “locale string”, because it is in a platform-dependent encoding depending on the current locale:
Some notes:
std::vector::data(), you can say&buf[0]instead.r-style conversion functionsmbsrtowcsandwcsrtombsdon’t work properly on Windows. There, you can use thembstowcsandwcstombsinstead:mbstowcs(buf.data(), cs, wn + 1);,wcstombs(buf.data(), cs, wn + 1);In response to your question, if you want to compare two strings, you can convert both of them to wide string and then compare those. If you are reading a file from disk which has a known encoding, you should use
iconv()to convert the file from your known encoding to WCHAR and then compare with the wide string.Beware, though, that complex Unicode text may have multiple different representations as code point sequences which you may want to consider equal. If that is a possibility, you need to use a higher-level Unicode processing library (such as ICU) and normalize your strings to some common, comparable form.