将字符串设置为固定空间?

时间:2016-03-22 15:55:47

标签: c# asp.net string

我想通过电子邮件发送一些车辆信息。 我有所有代码工作,但间隔信息是一个问题。每辆车都有一个清单,然后通过电子邮件发送清单。所以我遍历列表并获得缺陷和评论。

foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                defect += string.Format("{0,-40} {1}\n", item.ItemTitle, item.Comment);
            }
        }

        if (hasDefect == true)
        {
            Utils.ChecklistSendMail("Checklist", ToAddresses.Split(';'),
                "Vehicle Reg: " + reg + "\n" +
                "Checklist No: " + chkList.CheckListNo + "\n"+
                "Date: " + ChecklistDate.ToShortDateString() + "\n" +
                "Defects:                            Comments: " + "\n" + 
                defect);
        }

电子邮件然后看起来像这样:

Vehicle Reg: XLZ 8194
Checklist No: 0
Date: 22/03/2016
Defects:                   Comments: 
Vehicle Secure                 comment1
Brakes                     comment2

我希望缺陷和评论显示如下:

Defects:                     Comments: 
Vehicle Secure               comment1
Brakes                       comment2

由于Vehicle Secure超过Brakes,评论会被进一步推出。但是,无论第一个单词有多长,有没有办法将字符串固定在某个位置?

修改

代码现在看起来像这样:

    string defect = "";
        string comment = "";
        string aheading = "Defects:";
        string bheading = "Comments:";
foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                defect += item.ItemTitle;
                comment += item.Comment;
            }
        }

        string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine +
           defect.PadRight(20, ' ') + comment.PadRight(20, ' ') + Environment.NewLine;

但输出如下:

Defects:            Comments:            
Vehicle SecureBrakestest1test2 

2 个答案:

答案 0 :(得分:3)

如果你真的想用空格来做这件事,你需要确定具有最多字符的标签以及之后你想要多少空间。这会创建一个总字符数。从中,您减去标签的字符数,以获得排列该值所需的空格数。

但是,您可以使用<table>或其他一些html。

潜在,快速和肮脏的解决方案需要您生成html作为代码的一部分。我认真反对一些自产的html生成器逻辑。电子邮件中涉及的数据总是变得更加复杂。这导致混合代码获取模板的数据并构建html,这很难调试。此外还有很多html模板解决方案。你真的只是重新发明技术债务和维护更多代码。

更好的解决方案是使用类似MvcMailer的内容并构建一个html模板。然后,将模板和上下文对象传递给引擎以呈现生成的html。

答案 1 :(得分:1)

尝试使用带有''作为字符

的字符串填充
public string PadRight(
int totalWidth,
char paddingChar)

此方法将使用所选的char完成字符串的长度。通过指定字符串的最大长度并用“”(空格)替换剩余长度。你可以随时对齐字符串。

阅读有关PadRight或PadLeft

的更多信息

string Defects ="Example" Defects.PadRight(20," ");

结果:"Example "

编辑:示例代码。请查看此代码并检查您的错误

        string aheading = "Defects:";
        string bheading ="Comments:";
        string b = "Vehicle Secure";
        string bComment = "My Comment value";
        string c = "Brakes";
        string cComment = "My Comment value";


   string result= aheading.Trim().PadRight(20,' ')+bheading.Trim().PadRight(20,' ')+    Environment.NewLine +
       b.Trim().PadRight(20, ' ') + bComment.Trim().PadRight(20, ' ') + Environment.NewLine + 
       c.Trim().PadRight(20,' ')+cComment.Trim().PadRight(20,' ')+Environment.NewLine  ;

        Console.WriteLine(result);

编辑:根据您发布的代码回答

        string aheading = "Defects:";
        string bheading = "Comments:";


        string result = aheading.PadRight(20, ' ') + bheading.PadRight(20, ' ') + Environment.NewLine ;


        foreach (var item in chkList.CheckItems)
        {
            if (item.Defect == true)
            {
                string result += item.ItemTitle.Trim().PadRight(20,' ') +  item.ItemTitle.Trim().PadRight(20,' ') + Environment.NewLine ; 

            }
        }

        Console.WriteLine(result);
相关问题