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

文本框中的字符串怎么截取数字

2012-01-18 
文本框中的字符串如何截取数字比如“22,33,44,55,67”如何把每个数字都截出来,主要是对CString转换不太熟悉,

文本框中的字符串如何截取数字
比如“22,33,44,55,67”
如何把每个数字都截出来,主要是对CString转换不太熟悉,请高手指教,特别是用到那些函数。
多谢了!!

[解决办法]
循环用 CString 的Find方法找 ", "的位置, 然后 用CString的 Mid 方法 截取
[解决办法]
写好了,csSource 的 各种情况基本都测试了,包括单个多个数字非数字开头,结尾等。
csTemp 和 result 里面是取出的结果。
CString csSource = "2 2, 33, 4 4, 55, 6 7 ";
CString csTemp;
BOOL bNew = TRUE;
int hpos = 0;
int tpos = -1;
int i = 0;
int result = 0;
for (i = 0; i < csSource.GetLength(); ++i)
{
if ( !isdigit(csSource[i]) )
{
if ( bNew )
{
if ( hpos > = 0 && tpos > = 0 && hpos <= tpos )
{
csTemp = csSource.Left(tpos+1).Right(tpos-hpos+1);
result = atoi(csTemp);
}
bNew = FALSE;
}
continue;
}

if ( bNew )
{
tpos = i;
}
else
{
bNew = TRUE;
hpos = i;
tpos = i;
}
}
if ( bNew )
{
csTemp = csSource.Right(i-hpos);
result = atoi(csTemp);
}
[解决办法]
class SepStr
{
protected:
CString m_strOrig;
CString m_strRight;
intm_nPos;
CString m_strSep;

public:
SepStr(CString str,CString strSep= ", ")
{
m_nPos=-1;
m_strRight=m_strOrig=str;
m_strSep=strSep;
}
void SetSepString(CString str)
{
m_strSep=str;
}
CString Reset()
{
m_strRight=m_strOrig;
return m_strRight;
}
BOOL GetNext(CString &strOut)
{
if(m_strRight.GetLength()==0)
return FALSE;
m_nPos=m_strRight.Find(m_strSep);
if (m_nPos==-1)
{
strOut=m_strRight;
m_strRight= " ";
}
else
{
strOut=m_strRight.Left(m_nPos);
int nRight=m_strRight.GetLength()-m_nPos-m_strSep.GetLength();
m_strRight=m_strRight.Right(nRight);
}
return TRUE;
}



};

使用方法:
SepStr ss( "12;23;56 ", "; ");
CString temp;
while(ss.GetNext(temp))
{
}

热点排行