其实这个我一直在想最好还是用正则表达式实现,但发现效率有些不理想,而且这种情况下流程也复杂了
CSV每行记录中有时会有一个单元段数据中出现双引号的情况,
针对这种情况写一个函数,代码如下:
void GetCsvRecordColumnsData(CString record, CList<CString,CString&> & retList)
{
CList<CString,CString&> list;
retList.RemoveAll();
CString str =record;
int len = str.GetLength();
int iPos = str.Find(","), iStart = 0, offset = 0, findStartIndex = 0;
while (iPos != -1)
{
list.AddTail(str.Mid(iStart, iPos - iStart));
iStart = iPos + 1;
iPos = str.Find(",", iStart);
}
if (iStart <= len)
list.AddTail(str.Mid(iStart));
POSITION pos = list.GetHeadPosition();
CString preWords, curWord;
offset = 0;
while(pos != NULL){
curWord = (CString)list.GetNext(pos);
offset = 0;
if (curWord.Mid(0,1) == "/"" && curWord.Mid(curWord.GetLength()-1) != "/""){
preWords = curWord;
bool finded = false;
while( pos!=NULL ){
curWord = (CString)list.GetNext(pos);
++ offset;
if (curWord.Mid(0,1) != "/"" && curWord.Mid(curWord.GetLength()-1 ) == "/""){
curWord.Insert(0,",");
curWord.Insert(0,preWords);
retList.AddTail(curWord);
finded = true;
break;
}
else if (curWord.Mid(0,1) == "/"" && curWord.Mid(curWord.GetLength()-1) == "/""){
break;
}else if (curWord.Mid(0,1) == "/""){
break;
}else{
curWord.Insert(0,",");
curWord.Insert(0,preWords);
preWords = curWord;
}
}
if (! finded){
pos = list.FindIndex(findStartIndex);
if (pos!=NULL){
curWord = (CString)list.GetNext(pos);
retList.AddTail(curWord);
++findStartIndex;
}
}else{
findStartIndex += offset;
}
}else{
retList.AddTail(curWord);
++findStartIndex;
}
}
}
=================================
有其他方法的朋友分享出来吧。