2

Well the code explains what i am trying to do

auto haystack = wstring(L"\REGISTRY\MACHINE\SOFTWARE\WOW6432Node\Data\0\Path");
auto needle = wstring(L"\WOW6432Node\Data\0\Name");
auto found = haystack.find(needle) != haystack.npos;
cout << found << endl;

it returns true although needle is different than haystack notice the former ends with \Path while the latter ends with \Name... how could i exactly tell if certain string contains another ?

  • 2
    Instead of `\ `, try `\\ `. `\0` is terminating `0`. – AlexD Jun 26 '16 at 21:53
  • 2
    Those \ in the strings, they have a special meaning in C and C++ string literals. – juanchopanza Jun 26 '16 at 21:53
  • @AlexD this seems to be the answer, how could i ignore that and find a string in another normally like we can do in C# for instance with str.Contains(str2) ? – Ahmed T. Yousef Jun 26 '16 at 21:57
  • @AlexD stl strings can contain ` '\0'`s inside and they do not indicate end of string – mvidelgauz Jun 26 '16 at 21:59
  • IDK Why down voting what is wrong with the question ? – Ahmed T. Yousef Jun 26 '16 at 22:00
  • 3
    @mvidelgauz How does the constructor of `wstring` decide where the argument string ends? – AlexD Jun 26 '16 at 22:11
  • @AhmedT.Yousef What do you mean "how could I ignore that?" You can't ignore that. `L"\a\b\c\d\n"` means "a wide string containing a bell character, a backspace, the letter c, the letter d, and a newline". If you want a wide string containing backslash a backslash b backslash c backslash d backslash n, you write it as `L"\\a\\b\\c\\d\\n"`. – user253751 Jun 26 '16 at 22:12
  • @AlexD you are right! My bad... – mvidelgauz Jun 27 '16 at 07:33

3 Answers3

2

Due to the terminating '\0' the strings are shorter than you think:

auto haystack = std::wstring(L"\REGISTRY\MACHINE\SOFTWARE\WOW6432Node\Data");
auto needle = std::wstring(L"\WOW6432Node\Data");

The other unintended escape sequences should produce a compiler warning:

g++: warning: unknown escape sequence: '\R' (... and more)

You may look into: http://en.cppreference.com/w/cpp/language/string_literal

0

The \ character is the escape character. To use the actual \ character you need to escape it with \\. So your code needs to be changed like this:

auto haystack = wstring(L"\\REGISTRY\\MACHINE\\SOFTWARE\\WOW6432Node\\Data\\0\\Path");
auto needle = wstring(L"\\WOW6432Node\\Data\\0\\Name");
auto found = haystack.find(needle) != haystack.npos;
cout << found << endl;
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • what if i am working with unknown strings and i wanna do a normal check for a constant ? like str.Contains(str2) in C# ? – Ahmed T. Yousef Jun 26 '16 at 22:00
  • @AhmedT.Yousef, then you need a general way to escape your string. For example, take a look here: http://stackoverflow.com/questions/2417588/escaping-a-c-string – Lajos Arpad Jun 26 '16 at 22:17
  • No need to do any special escaping if your string is read from a file, the Registry, the keyboard, etc. You only need to escape *literals*. – Klitos Kyriacou Jun 26 '16 at 22:27
0

You can do it like this:

wstring haystack = wstring(LR"P(\REGISTRY\MACHINE\SOFTWARE\WOW6432Node\Data\0\Path)P");
wstring needle = wstring(LR"P(\WOW6432Node\Data\0\Name)P");
bool found = haystack.find(needle) != haystack.npos;
cout << (char*)(found?"True":"False") << endl;

\0 is a problem for you as it end constructor prematurely

Logman
  • 4,031
  • 1
  • 23
  • 35