如何格式化具有固定宽度字段的字符串

时间:2016-04-20 22:49:04

标签: c# .net string format

我想知道是否有一种格式化字符串格式化字符串的方法,我的意思是,我有一个foreach循环,它从一些文件中获取信息,每个文件有多少条记录,如每个文件的长度都不同,格式是变化的。

我的例子是,我有3个文件:

 1.- MyFile1.txt   RecordCount: 5
 2.- anotherfile.txt    RecordCount: 8
 3.- MyTestFile.doc   RecordCount: 17

正如你所看到的那样,我没有形成这样的东西:

 1.- MyFile1.txt        RecordCount: 5
 2.- anotherfile.txt    RecordCount: 8
 3.- MyTestFile.doc     RecordCount: 17

与文件的长度无关,RecordCount将在同一个地方。

我拥有的是:

 foreach (RemoteFileInfo file in MySession.EnumerateRemoteFiles(directory.RemoteDirectory, directory.RemoteFiles, EnumerationOptions.None))
 {
     BodyMessage.Append((index + 1) + ". " + file.Name + "        Record Count: " + File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString() + "\n");
     index++;
 }

有什么想法吗?

4 个答案:

答案 0 :(得分:4)

您可以尝试在字符串中使用\t来插入标签,也可以尝试填充每个部分,以便它们占用相同的空间。

例如:

string fileName = file.Name.PadRight(50);

将确保string fileName长度至少为50个字符。我说至少是因为你总是有一个大于50个字符的文件名。

答案 1 :(得分:2)

foreach (RemoteFileInfo file in MySession.EnumerateRemoteFiles(directory.RemoteDirectory, directory.RemoteFiles, EnumerationOptions.None))
{
    int lines= File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString();
    string appending = String.Format("{0,2}.- {1,-18} RecordCount: {3}", file.Name, lines);
    BodyMessage.Append(appending);
    index++;
}

请参阅MSDN: String.Format Method

答案 2 :(得分:0)

首先,使用string.Format,而不是连接:

import SpriteKit

class EncounterManager
{
    var encounterNames:[String] = []
    var currentEncounterIndex:Int = 0
    var previousEncounterIndex:Int = 0
    var encounters:[SKNode] = []
    var levelYpos:CGFloat = 60.0;


    func createNodes( encounterNames:[String])
    {
        self.encounterNames = encounterNames

        for encounterFileName in encounterNames
        {
            let encounter = SKNode()

            if let encounterScene = SKScene(fileNamed: encounterFileName) {

            for placeholder in encounterScene.children {

                let node = placeholder as SKNode

                switch node.name! {


                    // entities

                    case "Level-end":
                        let end = LevelEnd()
                        end.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:false);
                        break

                    case "Platform-left":
                        let platform = Platform()
                        platform.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:true);
                        break

                    case "Platform-right":
                        let platform = Platform()
                        platform.spawn(encounter, position:node.position, size:CGSize(width: 72, height: 33), useLeft:false);
                        break

                    case "Bat":
                        let bat = Bat()
                        bat.spawn(encounter, position: node.position)
                        break

                    case "Bee":
                        let bee = Bee()
                        bee.spawn(encounter, position: node.position)
                        break

要回答您的问题,您可以使用以下语法在格式化字符串中对齐文本:

int lineCount = File.ReadAllLines(Path.Combine(directory.LocalDirectory, file.Name)).Length.ToString();
string message = string.Format("{0}. {1}\t Record Count: " {2}\n", (index + 1), file.Name, lineCount);

额外的-10将确保插入的文本被填充为10个字符。

答案 3 :(得分:0)

我认为您可以为此使用PadRight或PadLeft函数。

string _line = item.FirstName.PadRight(20) + item.Number.PadRight(20) + item.State.PadRight(20)  + item.Zip.PadRight(20);
file.WriteLine(_line);