C语言字符串分割问题
strtok的函数原型为char *strtok(char *s, char *delim)
#include "stdio.h"
#include "string.h"
char str[20]="2020-3-9 20:19:19";
char *Fir_str,*Sec_str,*year,*month,*day;
int main(int argc, char const *argv[])
{
Fir_str=strtok(str," ");
Sec_str=strtok(NULL," ");
printf("Fir_str:%s\r\n",Fir_str);
printf("Sec_str:%s\r\n",Sec_str);
return 0;
}
运行结果:
#include "stdio.h"
#include "string.h"
char str[20]="2020-3-9 20:19:39";
char *Fir_str,*Sec_str;
char *year,*month,*day,*hour,*minute,*second;
int main(int argc, char const *argv[])
{
Fir_str=strtok(str," ");
Sec_str=strtok(NULL," ");
year=strtok(Fir_str,"-");
month=strtok(NULL,"-");
day=strtok(NULL,"-");
hour=strtok(Sec_str,":");
minute=strtok(NULL,":");
second=strtok(NULL,":");
printf("Fir_str:%s\r\n",Fir_str);
printf("Sec_str:%s\r\n",Sec_str);
printf("year:%s\r\n",year);
printf("month:%s\r\n",month);
printf("day:%s\r\n",day);
printf("hour:%s\r\n",hour);
printf("minute:%s\r\n",minute);
printf("second:%s\r\n",second);
return 0;
}