本文介绍了C# - 将可变数量的空间转换成字符串? (格式化输出文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alrighty,我是从一个列表,我填充一个DataGridView和我导出到一个文本文件中获取数据。我已经做了功能,将其导出为CSV,并希望做一个纯文本版本。

Alrighty, I'm taking data from a list that I populate a DataGridView with and am exporting it to a text file. I've already done the function to export it to a CSV, and would like to do a plain text version as well.

由于标题和其他元素的长度是可变的,当文件被保存,然后在记事本中打开它看起来像一个烂摊子,因为没有线了。

Because the Titles and other elements are variable in length, when the file is saved and then opened in Notepad it looks like a mess because nothing lines up.

我想有输出是这样的:

Sample Title One   Element One   Whatever Else
Sample Title 2     Element 2     Whatever Else
S. T. 3            E3            Whatever Else

我想,我可以通过各元素的循环,以获得最长的一个的长度,这样我就可以计算有多少空格添加到每个剩余的元素。

I figure that I can loop through each of the elements in order to get the length of the longest one so I can calculate how many spaces to add to each of the remaining element.

我的主要问题是:有一种优雅的方式来增加可变数量的字符转换为字符串这将会是不错的东西,如: myString.insert (索引,charToInsert,howManyToInsert);

My main question is: Is there an elegant way to add a variable number of chars into a string? It'd be nice to have something like: myString.insert(index, charToInsert, howManyToInsert);

当然,我可以明明只是写一个函数通过循环来做到这一点,但我想看看是否有这样做的更好的方法。

Of course, I can obviously just write a function to do this via a loop, but I wanted to see if there was a better way of doing it.

在此先感谢!

-Sootah

推荐答案

为此,您可能希望 myString.PadRight(totalLength,charToInsert)

请参阅 String.PadRight方法(Int32)以获得更多信息。

See String.PadRight Method (Int32) for more info.

这篇关于C# - 将可变数量的空间转换成字符串? (格式化输出文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 19:27