MFC基础--CString的Tokenize()和_tcstok()的用法对比

     Tokenize()和_tcstok()都是用来分割字符串的方法。但是其各自的使用还是有很多不同。

     下面对字符串“%s111gdfafd%s\t023232%s\t1%s\t2%s\t3%s\t4%s\t0XFF0000%s\tfdas”用这两个函数都进行一些相同匹配分割处理,代码和结果对比如下:    

Tokenize():

复制代码
#include "stdafx.h"
#pragma once

#include <stdio.h>
#include <tchar.h>
#include <vector>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // 某些 CString 构造函数将是显式的

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料
#endif

#include <afx.h>
#include <afxwin.h> // MFC 核心组件和标准组件
#include <iostream>//函数功能:按指定长度截取字符串前面的部分

int _tmain(int argc, _TCHAR* argv[], TCHAR* envp[])
{
CString sBuf=_T(" %s111gdfafd%s\t023232%s\t1%s\t2%s\t3%s\t4%s\t0XFF0000%s\tfdac");
CString Seperator = _T(“1%s\t”);
int Position = 0;
CString Token;

Token </span>=<span style="color: rgba(0, 0, 0, 1)"> sBuf.Tokenize(Seperator, Position);
</span><span style="color: rgba(0, 0, 255, 1)">while</span>(!<span style="color: rgba(0, 0, 0, 1)">Token.IsEmpty())
{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Get next token.</span>
    Token = sBuf.Tokenize(Seperator, Position);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">从iStart位置取出字符串中含pszTokens分割符间的内容;</span>
TCHAR* szTrunc = new TCHAR[Token.GetLength() + 1]; // 将结果保存在堆里 _tcscpy(szTrunc,Token); // 结果拷贝 std::wcout<<szTrunc<< std::endl;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (_tcslen(szTrunc) &gt; <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
    {
        delete [] szTrunc;
    }

}
system(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">pause</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;

}

复制代码
 

_tcstok():

复制代码
#include "stdafx.h"
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <vector>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // 某些 CString 构造函数将是显式的

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料
#endif

#include <afx.h>
#include <afxwin.h> // MFC 核心组件和标准组件
#include <iostream>//函数功能:按指定长度截取字符串前面的部分

int _tmain(int argc, _TCHAR* argv[], TCHAR* envp[])
{
CString str = _T("%s111gdfafd%s\t023232%s\t1%s\t2%s\t3%s\t4%s\t0XFF0000%s\tfdas");
TCHAR seps[] = _T(“1%s\t”);
TCHAR* token = _tcstok( str.GetBuffer(), seps );
while( token != NULL )
{
//MessageBox( token, token, MB_OK );
//MessageBox(_T(“dfzdsas”));
std::wcout<<token<<std::endl;
token = _tcstok( NULL, seps );//这一句删去会导致无限循环
}
system(“pause”);
return 0;
}

复制代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值