如何用空格填充一个字符串? [英] How to left pad a string with spaces?

查看:100
本文介绍了如何用空格填充一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个字符串说:


myvar [128] ="这是一个字符串" ;;


我该怎么办?使用sprintf转换字符串,使其有4个空格填充

左边像:


"这是一个字符串;

谢谢!

解决方案

nospam< no@spam.comwrites:


如果我有一个字符串说:


myvar [128] ="这是一个字符串;


如何使用sprintf转换字符串,使其有4个空格填充

左边像:


"这是一个字符串;



如果您的意思是就地 - 结果在同一个数组中结束 - 然后

你不能直接这样做,因为如果与

重叠对象一起使用sprintf是未定义的。


使用memmove和memset是IMO最简单的方法。


-

Ben。


nospam< no@spam.comwrote:


如果我有一个字符串说:


myvar [128] ="这是一个字符串;



我想这应该是


char myvar [128] ="这是一个字符串 ;


如何使用sprintf转换字符串,使其有4个空格填充

左边像:


"这是一个字符串;



sprint()没有转换字符串,它打印成一个字符串。

看起来好像你想使用sprintf ()''myvar''

作为来源和目的地,并且'不是pos /

sible。 C标准具体说明了这一点:


如果在重叠的对象之间进行复制,

行为是不确定的。


确切地解决了这种情况。所以没有办法

你可以用sprintf()可靠地做到这一点。


我想最后你想做一些更复杂的事情

但是对于你描述的你想做一个简单的


memmove(myvar + 4,myvar,strlen(myvar)+ 1);

memset(myvar,'''',4);


会做。注意使用memmove()代替memcpy(),因为源和目的地重叠,需要




问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\\ \\ __________________________ http://toerring.de


< blockquote> 2008年6月9日星期一23:28:17 +0000,Jens Thoms Toerring写道:


nospam< no@spam.comwrote:


>如果我有一个字符串说:


> myvar [128] ="这是一个字符串;



我想这应该是


char myvar [128] ="这是一个字符串 ;


>如何使用sprintf转换字符串,使其在左侧填充4个空格,如:


>"这是一个字符串;



sprint()没有转换字符串,它打印成一个字符串。

看起来好像你想使用sprintf ()''myvar''

作为来源和目的地,并且'不是pos /

sible。 C标准具体说明了这一点:


如果在重叠的对象之间进行复制,

行为是不确定的。


确切地解决了这种情况。所以没有办法

你可以用sprintf()可靠地做到这一点。


我想最后你想做一些更复杂的事情

但是对于你描述的你想做一个简单的


memmove(myvar + 4,myvar,strlen(myvar)+ 1);

memset(myvar,'''',4);


会做。注意使用memmove()而不是memcpy(),因为源和目的地重叠,所以需要




问候,Jens



我需要使用sprintf,类似于我正在使用的遗留代码。

这样的东西:


strncpy(fname,tmp + 21,9); fname [9] =''\ 0'';

strncpy(mname,tmp + 31,1); mname [1] =''\ 0'';


sprintf(tmp,"%4s%-20s%-20s%-32s%-4s",
"",fname,mname,lname,"");


If I have a string say:

myvar[128] = "This is a string";

How do I use sprintf to convert the string so it has 4 spaces padded on
the left like:

" This is a string";
Thanks!

解决方案

nospam <no@spam.comwrites:

If I have a string say:

myvar[128] = "This is a string";

How do I use sprintf to convert the string so it has 4 spaces padded on
the left like:

" This is a string";

If you mean "in place" -- the result ending up in the same array -- then
you can''t do it directly because sprintf is undefined if used with
overlapping objects.

Using memmove and memset is, IMO, the simplest way to do this.

--
Ben.


nospam <no@spam.comwrote:

If I have a string say:

myvar[128] = "This is a string";

I guess that''s supposed to be

char myvar[128] = "This is a string";

How do I use sprintf to convert the string so it has 4 spaces padded on
the left like:

" This is a string";

sprint() doesn''t convert strings, it prints into a string.
It looks as if you would like to use sprintf() with ''myvar''
as both the source and the destination and that''s not pos-
sible. The C standard says about this specifically:

If copying takes place between objects that overlap,
the behavior is undefined.

which exactly addresses this situation. So there''s no way
you can do this reliably with sprintf().

I guess in the end you want to do something more complicated
but for what you describe you want to do a simple

memmove( myvar + 4, myvar, strlen( myvar ) + 1 );
memset( myvar, '' '', 4 );

will do. Note the use of memmove() instead of memcpy() which
is required since source and destination overlap.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


On Mon, 09 Jun 2008 23:28:17 +0000, Jens Thoms Toerring wrote:

nospam <no@spam.comwrote:

>If I have a string say:

>myvar[128] = "This is a string";


I guess that''s supposed to be

char myvar[128] = "This is a string";

>How do I use sprintf to convert the string so it has 4 spaces padded on
the left like:

>" This is a string";


sprint() doesn''t convert strings, it prints into a string.
It looks as if you would like to use sprintf() with ''myvar''
as both the source and the destination and that''s not pos-
sible. The C standard says about this specifically:

If copying takes place between objects that overlap,
the behavior is undefined.

which exactly addresses this situation. So there''s no way
you can do this reliably with sprintf().

I guess in the end you want to do something more complicated
but for what you describe you want to do a simple

memmove( myvar + 4, myvar, strlen( myvar ) + 1 );
memset( myvar, '' '', 4 );

will do. Note the use of memmove() instead of memcpy() which
is required since source and destination overlap.

Regards, Jens


I need to use sprintf, similar to the legacy code I''m working with.
Something like this:

strncpy(fname,tmp+21,9); fname[9] = ''\0'';
strncpy(mname,tmp+31,1); mname[1] = ''\0'';

sprintf(tmp,"%4s%-20s%-20s%-32s%-4s",
" ",fname,mname,lname," ");


这篇关于如何用空格填充一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆