/* 字符串切割函数
strOperator代表需要提取的字符串,strSeq代表分隔符,
nHalfNum代表第一部分显示子串数,strFirstHalf第一部分显示
第二部分显示*/
/************************************************************************/int CShow_sqlDlg::CutUpOperatorNo(CString strOperator, CString strSeq, int nHalfNum, CString &strFirstHalf, CString &strSecondHalf)
{
int nIndex = 0;
//nHalfNum = 5;
strSecondHalf = strOperator;
for(int i = 0; i < nHalfNum; i++)
{
if(0 != strSecondHalf.GetLength()) //判断是否为空串
{
nIndex = strSecondHalf.Find(strSeq);
if(-1 != nIndex) //求取分割符后字符串
{
strSecondHalf = strSecondHalf.Right(strSecondHalf.GetLength() - nIndex - strSeq.GetLength());
}
else
{
strFirstHalf = strOperator;
strSecondHalf = _T("");
//AfxMessageBox(_T("字符串数不足,第一部分将为全部"));
return -1;
}
}
else
{
strFirstHalf = strOperator;
return -1;
}
}
strFirstHalf = strOperator.Left(strOperator.GetLength() - strSecondHalf.GetLength() - strSeq.GetLength());
return 1;
}
例如:
字符串为:11;22;33;44;55;66
当strSeq为‘;’, nHalfNum为5时
结果:第一部分 11;22;33;44;55;
第二部分 66
CString::find用法
//下面演示一个例子
// CString::Find( TCHAR ch )
CString s( "abcdef" );
int n = s.Find( 'c' ); // 结果 n = 2
int f = s.Find( "de" ) ; // 结果 f = 3
CString中left,Mid,Right用法
CString Left( int nCount ) const; //从左边1开始获取前 nCount 个字符
CString Mid( int nFirst ) const; //从左边第 nCount+1 个字符开始,获取后面所有的字符
CString Mid( int nFirst, int nCount ) const; //从左边第 nFirst+1 个字符开始,获取后面 nCount 个字符
CString Right( int nCount ) const; //从右边1开始获取从右向左前 nCount 个字符
//下面演示一个例子
CString a,b;
a = "123456789";
b = a.Left(4); //值为:1234
b = a.Mid(3); //值为:456789
b = a.Mid(2, 4); //值为:3456
b = a.Right(4); //值为:6789