【问题标题】:Extract a part of a string in c [duplicate]在c中提取字符串的一部分[重复]
【发布时间】:2014-12-11 13:06:44
【问题描述】:

如何仅从 char* 中提取日期部分?

   char *file = "TEST_DATA_20141021_18002.zip"

   char *date ="20141021" 

谢谢!

【问题讨论】:

  • 如果你的那个文件名的格式是一致的,你可以很容易地用strtok使用_作为分隔符...
  • @dragosht 给了你一个好主意。完成之后,找到一个由 8 个数字符号组成的标记,就是这样。
  • @dragosht 有两种文件名。 char *file ="TEST_DATA_BIG_20141021_1800211.zip"

标签: c string substring


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2012-03-28
    • 2018-11-07
    • 2015-02-02
    相关资源
    最近更新 更多