C++ Split Wide Char String

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 11:29:19

问题


I'm trying to split a WideChar String in to an array, Here is the way I'm doing this :<>

WCHAR* Message = _T("This is a sample text"):
wchar_t *pwc;
CStringArray Command;
pwc = wcstok(Message, L" ");
int Count = 0;
while (pwc != NULL) {
    pwc = wcstok(NULL, L" ");
    Command.Add(pwc);
    Count++;
}

for (int i = 0 ; i <= Count ; i++)
{
    AfxMessageBox(Command[i]);
}

The problem is I do not have "This" in my final result array
Whats wrong ?


回答1:


You need to move the call to Command.Add before the assignment to pwc in the loop - as it stands you're moving on to the second token before doing your first Add.




回答2:


I am having no issues with your source. All string components are printing perfectly.

Here's my full working code:

#include "stdafx.h"
#include <wchar.h>

int main(int argc, char *argv[])
{
    wchar_t wcs[] = L"This is a sample string";
    wchar_t *pwc;
    pwc = wcstok(wcs, L" ");
    while (pwc != NULL) {
        wprintf(L"%ls\n", pwc);
        pwc = wcstok(NULL, L" ");
    }
}


来源:https://stackoverflow.com/questions/19078574/c-split-wide-char-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!