如何从头开始截断char字符串并用C或C ++中的其他字符替换chars instring? [英] How to truncate char string fromt beginning and replace chars instring by other chars in C or C++?
问题描述
我有一个从ctime_r返回的日期时间字符串,它是
格式,如" Wed Jun 30 21 :49:08 1993\\\
\ 0",有26个字符
包括最后终止字符''\ 0'',我想删除
工作日信息是周三。在这里,我也想
用_替换空格char。并删除\ n焦炭。我没有知道如何从开始截断字符串或者用一个字符串替换一些字符
而不使用循环通过一个字符串
字符串的一个字符。我使用下面的代码来实现
替换 "通过_并删除了最后一个\ n char,没有
但即便如此,我仍然没有得到我喜欢的内容。下面是代码我
写道:
#include< time.h>
#include< stdio.h>
int main(无效)
{
time_t ltime;
char buf [50];
//获取时间
时间(& ltime);
// ctime_r返回的日期时间字符串格式为
//Wed Jun 30 21:49:08 1993\\\
\ 0
printf("时间:% s",ctime_r(& ltime,buf));
//替换 "和:在日期时间字符串中
//由_"
buf [7] =" _" ;; // ,第18行
buf [10] =" _" ;; // ,第19行
buf [13] =" _" ;; //":",第20行
buf [16] =" _" ;; //":",第21行
buf [19] =" _" ;; // ",第22行
buf [24] =" \0" ;; //删除最后的\ n字符,第23行
// printf新的datetimestring
printf("时间是:%s", (bf);
}
当我用gcc编写它时,我收到了以下警告:
test_ctimer.c:在函数`main'':
test_ctimer.c:18:警告:赋值从指针生成整数
没有演员
和第19到23行的警告也一样。
当我运行它时,我得到了:
时间是: 2008年8月7日星期四15:02:32
时间是:星期四8XX 7X15X02X32X2008Z
有人可以帮助我吗?我在互联网上搜索了它似乎C / /
库没有从头开始截断的功能?并且它还没有替换字符的功能。
也没有功能。我应该用
来使用循环吗?
非常感谢您的帮助。
Hongyu
Hi,
I have a datetime char string returned from ctime_r, and it is in the
format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars
including the last terminate char ''\0'', and i would like to remove the
weekday information that is "Wed" here, and I also would like to
replace the spaces char by "_" and also remove the "\n" char. I didn''t
know how to truncate the string from beginning or replace some chars
in a string with another chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn''t get what i like. Below is the code i
wrote:
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t ltime;
char buf[50];
// Get the time
time(<ime);
// The datetime string returned by ctime_r is in the format
// of "Wed Jun 30 21:49:08 1993\n\0"
printf("The time is: %s", ctime_r(<ime, buf));
// replace the " " and ":" in the datetime string
// by "_"
buf[7] = "_"; // " ", line 18
buf[10] = "_"; // " ", line 19
buf[13] = "_"; // ":", line 20
buf[16] = "_"; // ":", line 21
buf[19] = "_"; // " ", line 22
buf[24] = "\0"; // remove the last \n char, line 23
// printf the new datetimestring
printf("The time is: %s", buf);
}
When I complied it with gcc, i got the below warning:
test_ctimer.c: In function `main'':
test_ctimer.c:18: warning: assignment makes integer from pointer
without a cast
and same warning for line 19 to 23 too.
When I run it, I got:
The time is: Thu Aug 7 15:02:32 2008
The time is: Thu AugX 7X15X02X32X2008Z
Can anyone kindly help me? I searched on the internet and it seems C
library doesn''t have a function to truncate from the beginning? and it
also doesn''t have a function for characher replacement. Should I have
to use a loop?
Thanks a lot for the help in advance.
Hongyu
推荐答案
Hongyu写道:
Hongyu wrote:
我有一个从ctime_r返回的日期时间字符串,它是
格式,如&Wed; Wed Jun 30 21:49:08 1993\\\
\ 0,它有26个字符
,包括最后一个终止字符''\0'',我想删除
工作日信息,即Wed。在这里,我也想
用_替换空格char。并删除\ n焦炭。我没有知道如何从开始截断字符串或者用一个字符串替换一些字符
而不使用循环通过一个字符串
字符串的一个字符。我使用下面的代码来实现
替换 "通过_并删除了最后一个\ n char,没有
但即便如此,我仍然没有得到我喜欢的内容。下面是代码我
写道:
#include< time.h>
#include< stdio.h> ;
int main(无效)
{
time_t ltime;
char buf [50 ];
//获取时间
时间(& ltime);
//日期时间ctime_r返回的字符串格式为
//Wed Jun 30 21:49:08 1993\\\
\ 0
printf(&时间是:%s",ctime_r(& ltime,buf));
//替换 "和:在日期时间字符串中
//由_"
buf [7] =" _" ;; // ",第18行
Hi,
I have a datetime char string returned from ctime_r, and it is in the
format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars
including the last terminate char ''\0'', and i would like to remove the
weekday information that is "Wed" here, and I also would like to
replace the spaces char by "_" and also remove the "\n" char. I didn''t
know how to truncate the string from beginning or replace some chars
in a string with another chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn''t get what i like. Below is the code i
wrote:
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t ltime;
char buf[50];
// Get the time
time(<ime);
// The datetime string returned by ctime_r is in the format
// of "Wed Jun 30 21:49:08 1993\n\0"
printf("The time is: %s", ctime_r(<ime, buf));
// replace the " " and ":" in the datetime string
// by "_"
buf[7] = "_"; // " ", line 18
对于单个符号,您需要使用单引号:
buf [7] =' '_'';
(无处不在)。
For a single symbol you need to use single quotes:
buf[7] = ''_'';
(same everywhere).
buf [10] =" _" ;; // ,第19行
buf [13] =" _" ;; //":",第20行
buf [16] =" _" ;; //":",第21行
buf [19] =" _" ;; // ",第22行
buf [24] =" \0" ;; //删除最后的\ n字符,第23行
// printf新的datetimestring
printf("时间是:%s", (bf);
}
[..]
buf[10] = "_"; // " ", line 19
buf[13] = "_"; // ":", line 20
buf[16] = "_"; // ":", line 21
buf[19] = "_"; // " ", line 22
buf[24] = "\0"; // remove the last \n char, line 23
// printf the new datetimestring
printf("The time is: %s", buf);
}
[..]
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要''请问
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
8月7日,3:31 * pm,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:
On Aug 7, 3:31*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Hongyu写道:
Hongyu wrote:
Hi,
I有一个从ctime_r,*返回的日期时间字符串,它是
格式,如 1993年3月30日星期三21:49:08,有26个字符
包括最后一个终止字符''\ 0'',我想删除
工作日信息,即周三。在这里,我也想
用_替换空格char。并删除\ n焦炭。我没有知道如何从开头*截断字符串或者用一个字符串替换一些字符
而不使用循环通过一个字符/ >
字符串的一个字符串。我使用下面的代码来实现
替换 "通过_并删除了最后一个\ n char,没有
但即便如此,我仍然没有得到我喜欢的内容。下面是代码我
写道:
I have a datetime char string returned from ctime_r, *and it is in the
format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars
including the last terminate char ''\0'', and i would like to remove the
weekday information that is "Wed" here, and I also would like to
replace the spaces char by "_" and also remove the "\n" char. I didn''t
know how to truncate the string from beginning *or replace some chars
in a string with another chars without using a loop through one char
by one char of the string. I used the below code to achieve
replacement of " " by "_" and also removed the last "\n" char, without
considering removing the first 4 chars i.e. weekday information yet.
But even this I still didn''t get what i like. Below is the code i
wrote:
#include< time.h>
#include< stdio.h>
#include <time.h>
#include <stdio.h>
int main(void)
{
* * time_t ltime;
* * char buf [50];
int main(void)
{
* *time_t ltime;
* *char buf[50];
* * //获取时间
* * time(& ltime);
* *// Get the time
* *time(<ime);
* * // ctime_r返回的日期时间字符串格式为
* * //Wed Jun 30 21:49:08 1993 \\\
\ 0"
* * printf("时间是:%s",ctime_r(& ltime,buf));
* *// The datetime string returned by ctime_r is in the format
* *// of "Wed Jun 30 21:49:08 1993\n\0"
* *printf("The time is: %s", ctime_r(<ime, buf));
* * //替换 "和:在日期时间字符串中
* * //由_"
* * buf [7] =" _" ;; * // ",第18行
* *// replace the " " and ":" in the datetime string
* *// by "_"
* *buf[7] = "_"; *// " ", line 18
对于单个符号,您需要使用单引号:
* * * buf [7 ] =''_'';
(无处不在)。
For a single symbol you need to use single quotes:
* * *buf[7] = ''_'';
(same everywhere).
* * buf [10] =" _英寸; // ,第19行
* * buf [13] =" _" ;; //":",第20行
* * buf [16] =" _" ;; //":",第21行
* * buf [19] =" _" ;; // ,第22行
* * buf [24] =" \0" ;; //删除最后一个\ n字符,第23行
* *buf[10] = "_"; // " ", line 19
* *buf[13] = "_"; // ":", line 20
* *buf[16] = "_"; // ":", line 21
* *buf[19] = "_"; // " ", line 22
* *buf[24] = "\0"; // remove the last \n char, line 23
* * // printf新的datetimestring
* * printf(时间是:%s,buf);
* *// printf the new datetimestring
* *printf("The time is: %s", buf);
}
[..]
}
[..]
V
-
请在通过电子邮件回复时删除资金''A'
我不回复热门帖子回复,请不要问 - 隐藏引用的文字 -
- 显示引用的文字 -
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask- Hide quoted text -
- Show quoted text -
非常感谢提示帮助,维克多。有效!编译器
错误消失,空格被'' - ''取代。你能告诉我如何删除字符串开头的字符吗?以及如何
删除字符串中的字符,因为我在字符串中还有一个空格
字符串并想删除它。我尝试使用如下:
buf [0] ='''',但编译错误如:test_ctimer.c:18:12:空
字符常量
非常感谢。
Thanks a lot for the prompty help, Victor. It worked! The compiler
errors disappered and the space was replaced by ''-''. Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='''', but got compiler errors like: test_ctimer.c:18:12: empty
character constant
Thanks a lot.
Hongyu写道:
Hongyu wrote:
[ ..]你能告诉
我如何删除字符串开头的字符?以及如何
删除字符串中的字符,因为我在字符串中还有一个空格
字符串并想删除它。我尝试使用如下:
buf [0] ='''',但编译错误如:test_ctimer.c:18:12:空
字符常量
[..] Can you also tell
me how to remove the chars in the beginning of the string? and how to
remove a char inside the string, because i have one more space inside
the string and would like to remove it. I tried to use like:
buf[0]='''', but got compiler errors like: test_ctimer.c:18:12: empty
character constant
关于''memmove''函数的RTFM。它应该与重叠范围一起工作。
你可以记住部分字符串本身,IIRC。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
RTFM about ''memmove'' function. It should work with overlapping ranges.
You can ''memmove'' part of the string over itself, IIRC.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于如何从头开始截断char字符串并用C或C ++中的其他字符替换chars instring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!