关于listview读取csv文件过滤四列的问题
首先感谢ccrun版主的帮助,不然的话我还在我以前写的错误的代码里挣扎几天。现在我又有个问题,想把《...》中的数据过滤掉,后面的数据往前移动,
Designator,Footprint,Mid X,Mid Y,《Ref X,Ref Y,Pad X,Pad Y,》Layer,Rotation,Comment
,,,,,,,,,,
L1,C1005-0402,103.124mm,69.596mm,《103.124mm,69.596mm,103.124mm,68.946mm,》T,90,Inductor
JP1,HDR1X6,100.584mm,63.246mm,《100.584mm,69.596mm,100.584mm,69.596mm,》T,270,Header 6
即变成如下形式在listview中显示。
Designator,Footprint,Mid X,Mid Y,Layer,Rotation,Comment
,,,,,,,,,,
L1,C1005-0402,103.124mm,69.596mm,T,90,Inductor
JP1,HDR1X6,100.584mm,63.246mm,T,270,Header 6
以下代码是主要是ccrun版主帮忙写的,我加了个if else判断语句,但是调试明显不对,还请高手指点迷津?
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringList *lst = new TStringList;
lst->LoadFromFile("D:\\ccrun\\a.csv");
String strLine, str;
for (int i = 0; i < lst->Count; i++)
{
strLine = lst->Strings[i];
TListItem *li = ListView1->Items->Add();
while (strLine.Pos(","))//在一个字符串中查找另一个字符串所在位置
{
str = strLine.SubString(1, strLine.Pos(","));//返回第一参数开始直到第二个参数的子字符串
strLine = strLine.SubString(strLine.Pos(",") + 1, strLine.Length());
if (li->Caption.Length() < 1)
li->Caption = str;
else
{ if((NewItems->Caption.Length()>5) && (NewItems->Caption.Length()<9))
li->SubItems->Add('0');//‘0’表示我想去掉的列,调试了一段时间都正确。
else
li->SubItems->Add(str);
}
}
if (li->Caption.Length() < 1)
li->Caption = strLine;
else
li->SubItems->Add(strLine);
}
delete lst;
}
[解决办法]
我回来了。以下代码实现了过滤掉每行中《和》之间的数据,并且过滤掉逗号和mm字样。
TStringList *lst = new TStringList;lst->LoadFromFile("D:\\ccrun\\a.csv");String strLine, str;int nStart, nEnd;for (int i = 0; i < lst->Count; i++){ strLine = lst->Strings[i]; // 要确保每行数据中分隔符的开始和结束的匹配 nStart = strLine.Pos("《"); nEnd = strLine.Pos("》"); if (nStart && nEnd) // 这里的2是》的长度,如果换成其他的字符,这个长度也要更改 strLine = strLine.Delete(nStart, nEnd + 2 - nStart); TListItem *li = ListView1->Items->Add(); while (strLine.Pos(",")) { str = strLine.SubString(1, strLine.Pos(",") - 1); if (str.Length() > 2 && str[str.Length()] == 'm' && str[str.Length() - 1] == 'm') str = str.SubString(1, str.Length() - 2); strLine = strLine.SubString(strLine.Pos(",") + 1, strLine.Length()); if (li->Caption.Length() < 1) li->Caption = str; else li->SubItems->Add(str); } if (li->Caption.Length() < 1) li->Caption = strLine; else li->SubItems->Add(strLine);}delete lst;