建立在C#中固定宽度的文件 [英] Creating a fixed width file in C#

查看:148
本文介绍了建立在C#中固定宽度的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C#创建一个固定的宽度文件的最好方法。我有一个长一串字段写出来。说20,80.10,2等都左对齐。有没有一种简单的方法来做到这一点?

What is the best way to create a fixed width file in C#. I have a bunch of fields with lengths to write out. Say 20,80.10,2 etc all left aligned. Is there an easy way to do this?

推荐答案

您可以使用的String.Format轻松地垫用空格
例如,一个值

You can use string.Format to easily pad a value with spaces e.g.

string a = String.Format("|{0,5}|{1,5}|{2,5}", 1, 20, 300);
string b = String.Format("|{0,-5}|{1,-5}|{2,-5}", 1, 20, 300);

// 'a' will be equal to "|    1|   20|  300|"
// 'b' will be equal to "|1    |20   |300  |"

这篇关于建立在C#中固定宽度的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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