C#String.Format对齐 [英] C# String.Format Alignment

查看:356
本文介绍了C#String.Format对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有关于string.Format的问题



我使用以下代码,但我遇到了问题。我试图以一种格式打印字符串,其中每列相互排列但我遇到了问题。



  const   string  format =   | TG {0,6} | TO {1,4} | PO {2,4}

string result = string .Format(格式,净值,总价,价格);





请参阅下面链接中的文本文件,概述现在的样子以及我希望它看起来如何。



[链接删除]

文件内容:

a)看起来像是什么

< pre lang =text> | TG 98 | TO 41 | PO 42
| TG 101 | TO 42 | PO 42



b)我想要的是什么喜欢

 | TG 98 | TO 41 | PO 42 
| TG 101 | TO 42 | PO 42

[/ LINK DELETED]



然而似乎发生的事情就是在第1行我们在打印输出中有数字98和第2行101它显示98以101为中心。



这似乎是一致的,并且还抛出所有其他列。我确定代码没有问题,但有另一种方法可以实现我正在寻找的输出。



感谢帮助,

suprsnipes



删除文件链接并在问题中添加文件内容,使用代码块保存格式 - OriginalGriff [/编辑]

解决方案

看了你的我想要这样的文件,我不禁想到这不是你想要的 - 第一个看起来你在第二行的开头就有一个额外的空间,但是如果你在任何对齐的东西之后,它更可能是你想要的东西。



所以我尝试了你的代码:

  const   string  format =   | TG {0,6} | TO {1,4} | PO {2,4 }; 

Console.WriteLine( string .Format(格式, 98 101 42 ));
Console.WriteLine( string .Format(格式, 101 101 42 ));
Console.WriteLine( string .Format(格式, 8777 101 42 ));



出来的是完美对齐的:

 | TG 98 | TO 101 | PO 42 
| TG 101 | TO 101 | PO 42
| TG 8777 | TO 101 | PO 42

所以我怀疑问题出在其他地方......检查你用结果字符串做什么 - 可能是你输错了。


Hi,

I have a question regarding string.Format

I am using the following code but I am having a problem. I am trying to print the string in a format where each column lines up with one another but I''m having a problem.

const string format = "|TG {0,6} |TO {1,4} |PO {2,4}":

string result = string.Format(format, net, total, price); 



Please see the text file at the link below outlining what it looks like now and how I would like it too look.

[LINK DELETED]
File content:
a) what it looks like

|TG     98 |TO   41 |PO   42
 |TG    101 |TO   42 |PO   42


b) what I want it to loook like

|TG      98 |TO   41 |PO   42
|TG    101 |TO   42 |PO   42

[/LINK DELETED]

However what seems to happen is for example on line 1 we have the number 98 and line 2 101 in the print output it shows the 98 centered with 101.

This seems consistent across the board and throws all the other columns out as well. I''m sure there is no problem with the code but is there another way that I can acheive the output I am looking for.

Appreciate the help,
suprsnipes

[edit]Removed the file link and added the file content in the question, with code block to preserve the formatting - OriginalGriff[/edit]

解决方案

Having looked at your "I want it like this" file I can''t help thinking that isn''t what you want - the first one looks like you have an extra space at the start of the second line but it more likely to be what you want than the second if you are after anything that is aligned.

So I tried your code:

const string format = "|TG {0,6} |TO {1,4} |PO {2,4}";

Console.WriteLine(string.Format(format, 98, 101, 42));
Console.WriteLine(string.Format(format, 101, 101, 42));
Console.WriteLine(string.Format(format, 8777, 101, 42));


And what came out was aligned perfectly:

|TG     98 |TO  101 |PO   42
|TG    101 |TO  101 |PO   42
|TG   8777 |TO  101 |PO   42

So I suspect the problem is elsewhere...check what you do with your result string - it may be you are outputting it incorrectly.


这篇关于C#String.Format对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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