String的一个问题?

hello_wyq 2001-04-22 04:25:00
我有一个String s,内容为"1,2,3,4,5,6";
现在我想把他拆成整型,并放入一个数组中,比如:我的数组int a[6],我想把String中
的东西放入a【6】中,变成为a[0] = 1; a[1] = 2;...;a[5] = 6;有没有函数可以实现
我的要求,在这里分割符号为“,”,多谢!
...全文
69 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
猛禽 2001-04-22
  • 打赏
  • 举报
回复
如果是','就好办了:
TStrings ssTemp = new TStringList( );
ssTemp->CommaText = "1, 2, 3, 4, 5, 6";
int a[6];
for ( int i = 0; i < 6; i++ )
a[i] = StrToInt( ssTemp->Strings[i] );
delete ssTemp;
// 搞定了。
HuangBin 2001-04-22
  • 打赏
  • 举报
回复
补充一下:
X[I]//I的最小值是1,不是0
jiangsukid 2001-04-22
  • 打赏
  • 举报
回复
如果事AnsiString x;
StrToInt(x[i]);
如果事char x[10];
x[i]-'0'则可得到整形
millet 2001-04-22
  • 打赏
  • 举报
回复
好象没有什么现成的函数,我帮你写了一个。
int __fastcall DivisionString(AnsiString string,int * list)
{
int count = 0;
for (;;)
{
int pos=string.AnsiPos(",");
if (pos==0) //如果source 中没有子串 ","
{
if (string.IsEmpty())
{
break;
}
else
{
list[count] = string.ToInt();
count++;
break;
}
}
else
{
if (pos!=1)
{
AnsiString tmp = string.SubString(1,pos-1);
list[count] = tmp.ToInt();
count++;
}
string.Delete(1,pos);
}
}
return count;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString s = "1,2,3,4,5,6";
int a[6];
DivisionString(s,a);
for (int i=0;i<6;i++)
ShowMessage(a[i]);

}
//---------------------------------------------------------------------------
sundayboys 2001-04-22
  • 打赏
  • 举报
回复
好像没有直接的函数,用SubString、StrScan,StrScan查找“,”,用SubString来读出来。
natrium11 2001-04-22
  • 打赏
  • 举报
回复
自己编呀!每次找到下一个“,”,对子串做StrToInt()
懒得写代码了。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧