C ++ Split Wide Char String

时间:2013-09-29 13:28:41

标签: c++ string-split

我正在尝试将WideChar字符串拆分为数组,这是我这样做的方式:<>

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]);
}

问题是我的最终结果数组中没有“This” 怎么了?

2 个答案:

答案 0 :(得分:2)

您需要在分配到循环中的Command.Add之前将呼叫转移到pwc - 因为在您执行第一个Add之前,您正在转移到第二个令牌。

答案 1 :(得分:1)

我对您的来源没有任何问题。所有字符串组件都完美打印。

这是我的完整工作代码:

#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" ");
    }
}