0

CString每当有"\t"(制表符空格)字符时,我都会尝试将其一分为二。这是我正在使用的代码:

CString val = data->str;
CString valTok;
CString accTok;

int indx = 0;
valTok = val.Tokenize(_T("\t"), indx);
++indx;
accTok = val.Tokenize(_T("\t"), indx);

它很棒,但accTok缺少第一个字符。例如,when val = "String1\tString2", valTok = "String1"but accTok ="tring2"... 怎么会?

编辑:

所以按照 hvd 的建议,我删除了indx增量,我有以下代码:

CString val = ((CItemData*)lpDrawItemStruct->itemData)->str; // Was this value causing the problem in the end
CString valTok;
CString accTok;

int indx = 0;
valTok = val.Tokenize(_T("\t"), indx);
accTok = val.Tokenize(_T("\t"), indx); // ASSERT(iStart >= 0) fails

但是现在由于某种原因第一个Tokenize返回..!-1有任何想法吗?

解决了:

问题在于传递给的值val

((CItemData*)lpDrawItemStruct->itemData)->str 

有时,在某些情况下第二次NULL调用时会导致断言。Tokenize

4

1 回答 1

3

失去++indx.

CStringT::Tokenize的文档中:

iStart的值更新为结束分隔符之后的位置,如果到达字符串的结尾,则为 -1。

在第一次调用 之后Tokenizeindx已经指向了'\t',它已经指向了'S'“String2”。通过递增indx,它将指向t.

这是一个简单的健全性检查Tokenize

CString val;
CString str1, str2, str3;
int indx;

val  = _T("");
indx = 0;
str1 = val.Tokenize(_T("\t"), indx);
assert(str1 == _T("") && indx == -1);

val  = _T("String1");
indx = 0;
str1 = val.Tokenize(_T("\t"), indx);
assert(str1 == _T("String1") && indx >= 0);
str2 = val.Tokenize(_T("\t"), indx);
assert(str2 == _T("") && indx == -1);

val  = _T("String1\tString2");
indx = 0;
str1 = val.Tokenize(_T("\t"), indx);
assert(str1 == _T("String1") && indx >= 0);
str2 = val.Tokenize(_T("\t"), indx);
assert(str2 == _T("String2") && indx >= 0);
str3 = val.Tokenize(_T("\t"), indx);
assert(str3 == _T("") && indx == -1);

你得到不同的结果吗?

于 2012-09-24T16:20:43.967 回答