首页 > 解决方案 > c#无法在字符串中插入换行符

问题描述

我在下面的短信中插入换行符时遇到问题。目前我正在使用 Environment.NewLine 但这似乎不起作用。这就是它的显示方式。

在此处输入图像描述

代码

  error =
                            @"You cannot split a sale@
                              <ul>
                                <li>With yourself.</li>            
                                <li>A representative that has not completed their IBA and not been approved by compliance.</li>
                                <li>A terminated representative.</li>
                                </ul>".Replace("@", Environment.NewLine)

我基本上想这样展示它

You cannot split a sale

•   With yourself.            
•   A representative that has not completed their IBA and not been approved by compliance.
•   A terminated representative.

编辑

html

<div class="row">
      <div class="col-12 col-lg-6">
        <div *ngIf="infoMessage" class="notification warning">
          <fa-icon [icon]="['fas', 'info-circle']"></fa-icon>
          <span [innerHTML]="infoMessage"></span>
        </div>    
      </div>
    </div>

CSS

.notification {
  background: #d3d3d3;
  border-radius: 7.5px;
  margin: 0 0 15px;
  padding: 7.5px 10px;
  width: 100%;

  span {
    display: inline-flex;
    margin: 0 0 0 10px;
  }
}
.notification.success { background: $notification-success; color: $white; }
.notification.warning { background: $notification-warning; color: $white; }
.notification.error   { background: $pink; color: $white; }

标签: c#html

解决方案


你需要改变

.Replace("@", Environment.NewLine)

经过

.Replace("@", "<br />")

Environement.NewLine\r\n

这在 HTML 代码中毫无意义。 <br />是 HTML 代码中的等价物。

为什么使用<br />而不是\r\n,因为您的错误字符串似乎在您显示它时被解释,看起来像在网站上。\r\n在桌面应用程序中会很好。

编辑:或者您可以通过以下代码更新您的初始代码。

 error =
                            @"You cannot split a sale
<p>
                              <ul>
                                <li>With yourself.</li>            
                                <li>A representative that has not completed their IBA and not been approved by compliance.</li>
                                <li>A terminated representative.</li>
                                </ul></p>"

代码片段

You cannot split a sale
<p>
  <ul>
    <li>With yourself.</li>
    <li>A representative that has not completed their IBA and not been approved by compliance.</li>
    <li>A terminated representative.</li>
  </ul>
</p>

<h4> Same With BR</h4>
  You cannot split a sale
  <br />

  <ul>
    <li>With yourself.</li>
    <li>A representative that has not completed their IBA and not been approved by compliance.</li>
    <li>A terminated representative.</li>
  </ul>


推荐阅读