开发者

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

开发者 https://www.devze.com 2023-02-19 21:29 出处:网络
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.

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

精彩评论

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