简体   繁体   中英

Replacing Wchar_t

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM