【问题标题】:set string to a fixed space?将字符串设置为固定空间?
【发布时间】:2016-03-22 15:55:46
【问题描述】:

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

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

我希望缺陷和 cmets 显示如下:

Defects:                     Comments: 
Vehicle Secure               comment1
Brakes                       comment2

由于Vehicle SecureBrakes 长,评论被推得更远。但是,无论第一个单词有多长,有没有办法将字符串固定在某个位置?

编辑

代码现在看起来像这样:

    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 

【问题讨论】:

  • 您可以使用标签:"\t",但在您的电子邮件中使用 html 会更好。
  • 我认为以 html 格式发送电子邮件是最好的解决方案
  • @user1666620 我将如何在这里使用 html?
  • @user123456789 当然。您的邮件正文可以是纯 HTML

标签: c# asp.net string


【解决方案1】:

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

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

一个潜在的、快速而肮脏的解决方案需要您生成 html 作为代码的一部分。我强烈建议不要使用一些本土的 html 生成器逻辑。电子邮件中涉及的数据总是变得更加复杂。这导致混合获取模板数据和构建 html 的代码,调试起来很痛苦。还有很多 html 模板解决方案。你真的只是在重新发明轮子来承担技术债务和维护更多代码。

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

【讨论】:

  • 有没有办法将字符串包装成 html 或类似的东西?
  • 我想我明白你在问什么。我已经更新了我的答案。
  • 好的,谢谢您的回答。如果我使用<table>,我会怎么做?您能否举例说明我将如何在我的问题中使用 html 来编写代码
  • 您的问题是一般如何在 html 表格元素中放置文本?也许我不知道你在问什么。这是一个如何将文本放入表格中的示例w3schools.com/html/html_tables.asp
  • 是的,我了解 html 表格的基础知识,但我不确定如何在后面的代码中做到这一点。如何将我的代码放入表格中,然后将该表格放入电子邮件功能中?
【解决方案2】:

尝试使用字符串填充,以 ' ' 作为字符

public string PadRight(
int totalWidth,
char paddingChar)

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

阅读有关 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);

【讨论】:

  • 我在我的代码中添加了defect.PadRight(40, ' ');,但它并没有移行
  • 嗨,尝试将 ' ' 替换为 '* ' 只是为了测试,让我知道你得到了什么
  • 添加* 会导致错误:字符文字中的字符过多
  • 您在 'char' 之间似乎有多个字符。像这样使用'*'
  • 是的,我在 * 旁边添加了一个空格。这摆脱了错误。我编辑了我的问题,这样你就可以看到我想要做什么。 cmets仍然没有排队。如果您从这里查看示例 2,我会发现一些关于 padright 的示例,这就是我想要做的:dotnetperls.com/padright
猜你喜欢
  • 1970-01-01
  • 2017-11-05
  • 2016-01-31
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多