<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>
更多精彩资讯,请扫码关注