字符串格式问题 [英] string format question
问题描述
我需要格式化一个字符串,我想在一行中完成
char buff [32];
我需要点赞:
sprintf(buff," [% - 32s]",value); //空格键最多32个字符
但需要注意的是它不能为空终止。我确信有一种方式
- 我只是不知道它
所以,换句话说,我需要将一个字符串粘贴到预定义的缓冲区中,左边
对齐并用空格填充,总长度不超过32(
不能超过rmemory)并且它不能为空终止。
>
Christian
I need to format a string and I would like to do it in 1 line
char buff[32];
I need something LIKE:
sprintf(buff,"[%-32s]",value); // space pad up to 32 chars
with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don''t know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
Christian
推荐答案
christian.bongio ... @ gmail.com写道:
christian.bongio...@gmail.com wrote:
我需要格式化一个字符串,我想在一行中完成
char buff [32] ;
我需要类似的东西:
sprintf(buff,[% - 32s]",value); //空格键最多32个字符
但需要注意的是它不能为空终止。我确信有一种方式
- 我只是不知道它
所以,换句话说,我需要将一个字符串粘贴到预定义的缓冲区中,左边
对齐并用空格填充,总长度不超过32(
不能运行超过rmemory)并且它不能为空终止。
I need to format a string and I would like to do it in 1 line
char buff[32];
I need something LIKE:
sprintf(buff,"[%-32s]",value); // space pad up to 32 chars
with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don''t know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
for loop + strlen?
Tom
for loop + strlen?
Tom
2006年9月21日星期四,Tom St Denis写道:
On Thu, 21 Sep 2006, Tom St Denis wrote:
>
christian.bongio ... @ gmail。 com写道:
>
christian.bongio...@gmail.com wrote:
>我需要格式化一个字符串,我想在一行中完成
char buff [32] ;
我需要类似的东西:
sprintf(buff," [% - 32s]" value); //空格键最多32个字符
警告它不能为空终止。我确信有一种方式
- 我只是不知道它
所以,在语言中,我需要将一个字符串粘贴到一个预定义的缓冲区中,左边
对齐并填充空格,总长度不超过32(
不能运行超过记录),并且不得以空终止。
>I need to format a string and I would like to do it in 1 line
char buff[32];
I need something LIKE:
sprintf(buff,"[%-32s]",value); // space pad up to 32 chars
with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don''t know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
for loop + strlen?
for loop + strlen?
你不需要其中任何一个。只需这样做:
sprintf(buff,"% - 31s",value);
buff [31] ='''';
Tak-Shing
You don''t need either of them. Just do:
sprintf(buff, "%-31s", value);
buff[31] = '' '';
Tak-Shing
Tak-Shing Chan写道:
Tak-Shing Chan wrote:
On 2006年9月21日星期四,Tom St Denis写道:
On Thu, 21 Sep 2006, Tom St Denis wrote:
christian.bongio ... @ gmail.com写道:
christian.bongio...@gmail.com wrote:
我需要格式化一个字符串,我想在一行中完成
char buff [32];
我需要类似的东西:
sprintf(buff," [% - 32s]",value); //空格键最多32个字符
但需要注意的是它不能为空终止。我确信有一种方式
- 我只是不知道它
所以,换句话说,我需要将一个字符串粘贴到预定义的缓冲区中,左边
对齐并用空格填充,总长度不超过32(
不能运行超过rmemory)并且它不能为空终止。
I need to format a string and I would like to do it in 1 line
char buff[32];
I need something LIKE:
sprintf(buff,"[%-32s]",value); // space pad up to 32 chars
with the caveat it CANNOT be null terminated. I am sure there is a way
-- I just don''t know it
So, in words, I need to stick a string into a predefined buffer, left
justified and padded with spaces, total length no longer than 32 (to
not run ove rmemory) and it must not be null terminated.
for loop + strlen?
for loop + strlen?
你不需要其中任何一个。只需这样做:
sprintf(buff,"% - 31s",value);
buff [31] ='''';
Tak-Shing
You don''t need either of them. Just do:
sprintf(buff, "%-31s", value);
buff[31] = '' '';
Tak-Shing
我认为如果价值是> = 32个字符,不需要填充,所以
这似乎不符合OP的要求。
我'' d说写出自己的功能是正确的方法。
这样可行。它没有经过测试,而且strncpy是浪费的,但是...
strncpy(buff,value,32);
if(buf [31] ==''\ 0''){
size_t len = strlen(buf);
memset(buf + len,''' ',32-len);
}
-David
I think if the "value" is >= 32 characters, NO padding is wanted, so
this doesn''t seem to meet OP''s requirements.
I''d say that writing his own function to do this is the proper way.
This would work. It isn''t tested, and the strncpy is wasteful, but...
strncpy(buff, value, 32);
if (buf[31] == ''\0'') {
size_t len = strlen(buf);
memset(buf+len, '' '', 32-len);
}
-David
这篇关于字符串格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!