首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VC >

VC++.NET瓜分字串

2012-12-25 
VC++.NET分割字串在VS2008,WinForm中,我要将一个textBoxCoe-Text的值传递到一个double dCoe[]中。由于文本

VC++.NET分割字串
在VS2008,WinForm中,我要将一个textBoxCoe->Text的值传递到一个double dCoe[]中。由于文本是以','分隔两个数,因为需要分割字串。
我做了两种尝试,不过都失败了。
方法一:
虽然在C++里没有Split(),但textBoxCoe->Text是System::String类,因此有Split.
String^ sTmp = textBoxCoe->Text;
String^ sCoe[] = sTmp.Split(",");       //这是第189行
报错:
错误2error C2664: “cli::array<Type,dimension> ^System::String::Split(...cli::array<wchar_t,1> ^)”: 不能将参数 1 从“const char [2]”转换为“wchar_t”f:\ch\vs_projects\高斯消元法\高斯消元法\Form1.h189
错误1error C2728: “System::String ^”: 本机数组不能包含此托管类型f:\ch\vs_projects\高斯消元法\高斯消元法\Form1.h189
方法二:
非托管的string类有find 函数。
string  sTmp;
sTmp = (char *)(void *)Marshal::StringToHGlobalAnsi( textBoxCoe->Text );//把System::String强制转换为string
string * sCoe;
sCoe = new string [nDim*nDim];
int nPos1 = 0;
int nPos2 = 0;//记录','位置
for( int i = 0; i<nDim*nDim; i++ )
{
nPos2 = sTmp.find(",",nPos1);
sCoe[i] = sTmp.substr( nPos1,nPos2-nPos1 );                    //记录应数字字串
nPos1 = nPos2;
dCoe[i] = atof( sCoe[i] );                                     //转化为double型,传入数组中
i++;
}
报错:
错误1error C2664: “atof”: 不能将参数 1 从“std::string”转换为“const char *”f:\ch\vs_projects\高斯消元法\高斯消元法\Form1.h174




无奈了,请各位大侠们帮帮忙了!


[解决办法]
托管可以用
Double::parse(System::String^)
非要用 atof 的话直接 std::string 的 c_str()成员函数

热点排行