简体   繁体   English

如何从c中的日期时间字符串中删除空格

[英]How to remove spaces from date time string in c

The code below creates a string with date and time for example Wed Jul 26 14:45:28 2017 下面的代码创建一个带有日期和时间的字符串,例如Wed Jul 26 14:45:28 2017

How could I remove the spaces from it? 我怎样才能删除其中的空格? So that is is WedJul2614:45:28 ? 那就是WedJul2614:45:28吗?

Original code: 原始代码:

#include <stdio.h>
#include <time.h>

int main() {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[64];
    strftime(s, sizeof(s), "%c", tm);
    printf("%s\n", s);
}

I tried this code but it prints wed?July 我尝试了此代码,但打印wed?July

#include <stdio.h>
#include <time.h>

int main() {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[64];
    char temp[64];
    strftime(s, sizeof(s), "%c", tm);
    printf("%s\n", s);


    for (int i = 0; i < sizeof(s); i++) {
      if (s[i] != ' ') {
        temp[i] = s[i];
      }
    }
printf("%s\n", temp);  
}
int j = 0;
for (int i = 0; s[i]!='\0'; i++) {
  if (s[i] != ' ') {
    temp[j] = s[i];
    j++;
  }
}

Keep track of the index so you don't just leave the spaces with some random value. 跟踪索引,以便您不只是给空格留一些随机值。 Also, you should add a null at the end of temp. 另外,您应该在temp的末尾添加一个null。

temp[j] = '\0';

Matt's answer works but you can do it too: 马特的答案有效,但您也可以这样做:

This exemple is without call to system function: strlen. 该示例无需调用系统功能:strlen。 Here we don't need it. 在这里我们不需要它。 It's a bit optimize. 有点优化。

i = 0;
int j = 0;
while (s[i] != '\0')
  {
    if (s[i] == ' ')
      {
        temp[j] = s[i];
        j++;
      }
    i++;
  }
temp[j] = '\0';

Don't forget the '\\0' at the end of your string. 不要忘记字符串末尾的“ \\ 0”。

Matt already provided an answer for what you asked . 马特已经为您的要求提供了答案。

What you probably want or need can also be achieved a bit easier. 您可能想要需要的也可以轻松实现。 If you don't want spaces, just avoid adding them in the first place: 如果您不想要空格,只需避免首先添加空格:

Replace your format string "%c" for strftime() which provides standard format for your locale with a string that creates directly what you want: "%a%b%d%T" 用直接创建所需内容的字符串替换strftime()的格式字符串"%c" ,该字符串为您的语言环境提供标准格式: "%a%b%d%T"

A bit more expensive, but you at least do not need another variable and as well have a valid string on each modification. 稍微贵一点,但是您至少不需要另一个变量,并且每次修改都具有有效的字符串。

#include <string.h>

...

  size_t l = strlen(s);
  for (size_t i = 0; i < l; ++i) {
    if (s[i] != ' ') {
      memmove(s+i, s+i+1, l-i+1); 
    }
  }

You can squeeze spaces out in place, using standard library functions. 您可以使用标准库函数将空间压缩到位。 Given that the string is very short and there's only a few spaces, performance won't suffer much. 鉴于字符串非常短且只有几个空格,因此性能不会受到太大影响。

#include <stdio.h>
#include <time.h>
#include <string.h>

int main() {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[64];
    strftime(s, sizeof(s), "%c", tm);
    printf("before: %s\n", s);

    for ( char *p = strchr(s, ' '); p ;  p = strchr(p, ' ') )
        strcpy(p, p+1);

   printf("after: %s\n",s);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM