【记录】字符串分割(C++)

 

<span style="font-family:Comic Sans MS;font-size:18px;">char *strtok(
   char *strToken,
   const char *strDelimit 
);
char *strtok_s( 
char *strToken, 
const char *strDelimit,
   char **context 
); </span>

 

 

 

 

 

 

Parameters

strToken

String containing token or tokens.

strDelimit

Set of delimiter characters.

context

Used to store position information between calls to strtok_s

 

使用strtok(C语言函数)实现字符串的分割

 

<span style="font-family:Comic Sans MS;font-size:18px;">#include <string.h>
#include <stdio.h>

int main()
{
        char s[] = "Golden Global      View,disk * desk";
         const char *d = " ,*";
         char *p;
        p = strtok(s,d);
         while(p)
         {
                 printf("%s\n",p);
                 p=strtok(NULL,d);
         }
        return 0;
}</span>

 

 

 

 

 

 

使用strtok_s(Windows函数)实现字符串的分割

<span style="font-family:Comic Sans MS;font-size:18px;">#include <stdio.h>
#include <string.h>

int main()
{
	char s[] = "Golden Global      View,disk * desk";
	const char *d = " ,*";
	char *p, *pNext;
	p = strtok_s(s, d, &pNext);
	while (p)
	{
		printf("%s\n", p);
		p = strtok_s(NULL, d, &pNext);
	}
}</span>

 

 

<span style="font-family:Comic Sans MS;font-size:18px;">char t[] = "a,b,c,de";
	const char *split = ",";
	char *p = NULL, *pNext = NULL;
	p = strtok_s(t, split, &pNext);
	CString R;
	while (p != NULL){
		R += p;
		R += L"\r\n";
		R += L"-----------------";
		R += L"\r\n";
		p = strtok_s(NULL, split, &pNext);
	}
	this->MessageBox(R);</span>


使用_tcstok_s分割字符串(CString版)

 

<span style="font-family:Comic Sans MS;font-size:18px;">Split(CString strSource, CStringArray& strDest)
{
	strDest.RemoveAll();
	TCHAR seps[] = _T("\r\n");
	TCHAR *token, *next_token;
	token = _tcstok_s((LPTSTR)(LPCTSTR)strSource, seps, &next_token);
	while (token != NULL)
	{
		strDest.Add(CString(token));
		token = _tcstok_s(NULL, seps, &next_token);
	}
}</span>

更多精彩资讯,请扫码关注

qr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值