请帮我截取一下字符串好吗?40分,谢谢

maya8maya85 2016-07-21 09:16:17
我有一字符串
string s = L"1=我的|2=你的|3=mykill";


另有一个函数,
while(...)
{
SetTag(x); //x的数据类型为int,把字符串中的1,2,3放在这里
SetText(y); //y的数据类型为LPCTSTR,把字符串中的“我的”,“你的”,“mykill”放在这里
}

求写出实际代码。
...全文
162 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
没人会吗?版主们
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
引用 4 楼 fengqinqdca 的回复:
AfxExtractSubString这个函数可以按照"|"把你的字符串分割三等份,其他的你用CString自己处理吧。 很简单 C语言里面还有个strtok也是分解字符串的。
我这个是Win32项目,忘记说了。
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
引用 5 楼 VisualEleven 的回复:

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);
我这个是Win32项目,忘记说了。
Eleven 2016-07-21
  • 打赏
  • 举报
回复

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);
华美乐章 2016-07-21
  • 打赏
  • 举报
回复
AfxExtractSubString这个函数可以按照"|"把你的字符串分割三等份,其他的你用CString自己处理吧。 很简单 C语言里面还有个strtok也是分解字符串的。
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
没有人吗?。。。。
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
引用 1 楼 chengbar 的回复:
先用|分割, 再scanf("%d=%s")
能写出来吗?
sevancheng 2016-07-21
  • 打赏
  • 举报
回复
先用|分割, 再scanf("%d=%s")
「已注销」 2016-07-21
  • 打赏
  • 举报
回复
引用 10 楼 maya8maya85 的回复:
你是好样的,请问一下,你使用是do while,我用是while,有什么区别呢?
StrChr 最后一次返回 NULL,但是还是需要处理的。这个也可以改成 while 的,看你怎么写了。
maya8maya85 2016-07-21
  • 打赏
  • 举报
回复
引用 9 楼 SXJIAKE 的回复:
最基本的字符串分割:
#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...
你是好样的,请问一下,你使用是do while,我用是while,有什么区别呢?
「已注销」 2016-07-21
  • 打赏
  • 举报
回复
最基本的字符串分割:
#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...

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧