数据循环
现在想实现将一段包含空格的字符串如“56 65 67 89” 分别付值给不同的字符串,现在只实现了一个最后的89赋值给Memo1->Text,包含空格的字符串长度是不确定的,不知道怎么用循环 ,大侠帮忙给指导下
a=str.Pos(" ");
while(a!=0)
{
str1=str.SubString(1,a-1);
Memo1->Text=str1;
str.Delete(1,a);
a=str.Pos(" ");}
}
[解决办法]
String str = "56 65 67 89 100";String strTemp;while (str.Length() > 0){ if (str.Pos(" ") > 0) strTemp = str.SubString(1, str.Pos(" ") - 1); else strTemp = str; if (strTemp.Trim().Length() > 0) Memo1->Lines->Add(strTemp.Trim()); str = str.Delete(1, strTemp.Length()); str = str.TrimLeft();}
[解决办法]
String str = "56 65 67 89 100";
TStringList *sg = new TStringList();
ExtractStrings(TSysCharSet() << ' ', TSysCharSet() << ' ', str.c_str(), sg);
for(int i = 0; i < sg->Count; i++)
{
TMemo *memo = (TMemo *)(this->FindComponent("Memo" + IntToStr(i + 1)));
if(memo)
memo->Text = sg->Strings[i];
}
delete sg;