在c中提取字符串的一部分

时间:2014-10-16 08:36:53

标签: c string substring

如何仅从char *?

中提取日期部分
   char *file = "TEST_DATA_20141021_18002.zip"

   char *date ="20141021" 

谢谢!

1 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <ctype.h>

int main(){
    char *file = "TEST_DATA_20141021_18002.zip";
    char date[16];
    char *s = file, *d = date;
    while(!isdigit(*s))
        ++s;
    do{
        *d++ = *s++;
    }while(isdigit(*s));
    *d = 0;
    puts(date);
    //or
    sscanf(file, "%*[^0-9]%15[0-9]", date);//0-9 : 0123456789
    puts(date);
    return 0;
}