【问题标题】:Embed newlines in a string在字符串中嵌入换行符
【发布时间】:2021-06-11 06:53:56
【问题描述】:
protected void Button1_Click(object sender, EventArgs e)
        {
            result.Text = "Hello! Here is your school information:" <br />
            "Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
            "Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
            "Address: " + add.Text +
            "Age: " + age.Text +
            "Contact Information: " + num.Text + " / " + email.Text + "";

        }

如何在此处添加换行符?例如;

全名:比尔·盖茨

课程、年份和部门:

就像那个格式

【问题讨论】:

  • 你可以使用\n
  • 尝试阅读StringBuilder类。它可能会帮助您了解如何构造字符串。还可以查看它的成员,例如 AppendAppendLine
  • 您还需要html encode动态值,以防有人将跨站点脚本攻击放入其中一个字段。
  • 欢迎使用 stackoverflow。我推荐taking the tour,以及阅读how to ask a good questionwhat's on topic。还有一项非常重要的技能:使用搜索引擎。例如,您的问题可以通过谷歌搜索 c# add newline 得到回答
  • 一个换行,还是一个HTML &lt;br&gt;元素?它们是两种不同的东西。您似乎已经尝试添加 &lt;br&gt; 元素。您是否遇到某种错误?你怎么看待这个错误?您在字符串中包含“
    ”的尝试与您在字符串中包含的所有其他内容有何不同?

标签: c# asp.net newline


【解决方案1】:

我通常使用Environment.NewLine,或者你可以添加字符串“\r\n”或“\n”(取决于系统)。更多here

编辑: cmets中提到,如果是网页输出,则换行的html标签为&lt;br&gt;。但是对于 C# 它只是字符串,所以你的代码应该是

protected void Button1_Click(object sender, EventArgs e)
{
    result.Text = "Hello! Here is your school information:" + "<br />" +
    "Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
    "Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
    "Address: " + add.Text +
    "Age: " + age.Text +
    "Contact Information: " + num.Text + " / " + email.Text + "";
}

【讨论】:

  • 新行?在网页上?
  • 我错过了有关 asp.net 的信息,请随时编辑我的答案或添加新答案。我不是 asp.net 开发人员,对我来说 result 只是一些领域。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-07
  • 1970-01-01
  • 2017-11-14
  • 2013-05-23
  • 1970-01-01
  • 2011-01-22
  • 2014-03-11
相关资源
最近更新 更多