开发者

how to print strings on definite number of bits in C#?

I am using VS 2010 - WPF - C#

I can't find out how to specify the number of bits that will be reserved for the string when its printed on the screen.

Back in Pascal we used to do Writeline("some string here"):20 - and the string w开发者_JAVA技巧ould be printed only on 20 bits.

How would I do this in C#?


This may be a close equivalent

string.Format("{0,20}","some string here")

EDIT:

For truncation, you could do

int length = 20;
string.Format("{0,"+length+"}", "some string here".Substring(0,length));

or if you don't like string concatenation "{0,"+length+"}" you can try

string.Format(string.Format("{{0,{0}}}", length), "some string here".Substring(0, length));


Not specific to writing to the screen, but you can get a substring of any string and use that for outputting:

string myString = "Something very long to be put here";

// Output just the first 10.
Console.WriteLine(myString.Substring(0,10));


So in WPF : <TextBlock Text="{Binding SomeString, StringFormat={0,20}}" />

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜