How can I replace string in a wchar_t
variable?
wchar_t text[] = L"Start Notepad.exe";
I want to replace Notepad.exe
with Word.exe
.
How can I replace string in a wchar_t
variable?
wchar_t text[] = L"Start Notepad.exe";
I want to replace Notepad.exe
with Word.exe
.
wchar_t is just one character, not the string. In your case you need std::wstring, string consisting of wchar_t. Here is answer on how to replace one substring with another.
Basically you need to use a pointer or index variable and replace the characters one by one and end with the null character.
Here is an example, (note that C++ programmers might hate this code, but is fairly acceptable for C programmers...):
wchar_t lpSrcText[] = L"Start Notepad.exe";
wchar_t const * lpReplace = L"Word.exe";
int i = 0;
while(*(lpReplace + i))
{
lpSrcText[6 + i] = *(lpReplace + i);
i++;
}
*(lpReplace + i) = L'\0';