16,471
社区成员
发帖
与我相关
我的任务
分享
string s = L"1=我的|2=你的|3=mykill";
while(...)
{
SetTag(x); //x的数据类型为int,把字符串中的1,2,3放在这里
SetText(y); //y的数据类型为LPCTSTR,把字符串中的“我的”,“你的”,“mykill”放在这里
}
BOOL GetTagText(CString& strTag, CString& strText, const CString& strInfo)
{
BOOL bRet = FALSE;
do
{
if(strInfo.IsEmpty()) { break; }
CString strTemp(TEXT(""));
LPCTSTR lpszToken = TEXT("=|");
int curPos = 0;
strTemp = strInfo.Tokenize(lpszToken, curPos);
int nIndex = 0;
while(TEXT("") != strTemp)
{
if(++nIndex % 2) { strTag += strTemp; }
else { strText += strTemp; }
strTemp = strInfo.Tokenize(lpszToken, curPos);
}
// Completed
bRet = TRUE;
} while (FALSE);
return bRet;
}
// 调用代码:
CString strTag;
CString strText;
GetTagText(strTag, strText, TEXT("1=我的|2=你的|3=mykill"));
AfxMessageBox(strTag);
AfxMessageBox(strText);
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
int main(
int argc,
char *argv[]
)
{
TCHAR szSource[] = _T("1=我的|2=你的|3=mykill");
int nIndex;
TCHAR szInfo[256];
TCHAR szItem[256];
LPCTSTR pszLine;
LPCTSTR pszStart = szSource;
do {
pszLine = StrChr(pszStart, _T('|'));
if (pszLine != NULL)
{
int nLength = pszLine++ - pszStart;
lstrcpyn(szItem, pszStart, nLength + 1);
pszStart = pszLine;
}
else
{
lstrcpy(szItem, pszStart);
}
printf("%s\n", szItem);
LPCTSTR pszEqual = StrChr(szItem, _T('='));
if (pszEqual == NULL)
{
break;
}
nIndex = StrToInt(szItem);
lstrcpy(szInfo, pszEqual + 1);
printf("%d\n", nIndex);
printf("%s\n", szInfo);
printf("\n");
} while (pszLine != NULL);
return 0;
}
输出:1=我的
1
我的
2=你的
2
你的
3=mykill
3
mykill
Press any key to continue...