简体   繁体   English

在字符串C#末尾删除几个字符的正确方法是什么

[英]What is correct way to a remove few characters at the end of string C#

Consider the following code, what is the correct way to a remove the last two <br/> tags from the end of string in C#? 考虑下面的代码,什么是正确的方式来一个删除最后两个<br/>从字符串在C#中结束标记? I have followed very naive method, to achieve this. 我遵循非常天真的方法,实现这一目标。 Can you please suggest improvements. 你能否提出改进建议?

List<string> MessageList; // populated from data source
Label lblHtmlOutput = new Label();
StringBuilder sb = new StringBuilder();
foreach (var item in MessageList)
{
    sb.Append(item + "<br/><br/>");
}
sb.Remove(sb.Length - 11, sb.Length - 1);

Don't add them in the first place. 首先不要添加它们。 Use something like: 使用类似的东西:

String.Join("<br/><br/>", MessageList);

Don't insert them in the first place: 不要在第一时间插入它们:

    List<string> MessageList; // populated from data source
    Label lblHtmlOutput = new Label();
    //StringBuilder sb = new StringBuilder();
    //foreach (var item in MessageList)
    //{
    //    sb.Append(item + "<br/><br/>");
    //}
    //sb.Remove(sb.Length - 11, sb.Length - 1);
    string list = string.Join("<br/><br/>", MessageList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM