在网络通信里经常会遇到一大串消息以特定分隔符连在一起的消息窜,所以分离消息会经常遇到。
第一个参数表示收到的消息窜,第二个参数表示所要分离的消息位置(从零开始),第三个参数表示存放到哪里,第四个参数表示特定分隔符,第五个参数表示分隔符长度
返回分隔符个数
<pre class="cpp" name="code">int nSize=ZHSplitGetSubText<span style="font-family: Arial, Helvetica, sans-serif;">(csValue, 0,strRoomMac,_T("_"));</span>
ZHSplitGetSubText<span style="font-family: Arial, Helvetica, sans-serif;">(csValue, 1,strRoomIP,_T("_"));</span>
<span style="font-family: Arial, Helvetica, sans-serif;">//头文件</span>
<span style="font-family: Arial, Helvetica, sans-serif;">DWORD ZHSplitGetSubText(CString& csText, int nIndex, CString& csSub, LPCTSTR strBar=_T(","),int nBarSize=1);
</span>
<span style="font-family:Arial, Helvetica, sans-serif;">//CPP文件</span>
DWORD CYourClassName::ZHSplitGetSubText(CString& csText, int nIndex, CString& csSub, LPCTSTR strBar,int nBarSize)
{
DWORD nReturn=0;
CString strTmp=csText;
csSub = _T("");
int i = 0;
int nOffsetBegin = 0, nOffsetEnd = 0;
for(i = 0; i <= nIndex; i++)
{
if(nOffsetEnd > 0)
nOffsetBegin = nOffsetEnd + 1;
nOffsetEnd = csText.Find(strBar, nOffsetBegin);
if(nOffsetEnd == -1)
break;
}
<span style="white-space:pre"> </span>if((nOffsetEnd == -1) && (i == nIndex))
<span> </span>{
<span> </span>csSub = csText.Right(csText.GetLength() - nOffsetBegin);
<span> </span>}
<span> </span>else if(nOffsetEnd != -1)
<span> </span>{
<span> </span>csSub = csText.Mid(nOffsetBegin, nOffsetEnd - nOffsetBegin);
<span> </span>}
int n=strTmp.Find(strBar);
while (n>0)
{
nReturn++;
strTmp=strTmp.Right(strTmp.GetLength()-n-nBarSize);//
n=strTmp.Find(strBar);
}
return nReturn;
}