【问题标题】:Trim a multiline String in c# [duplicate]在c#中修剪多行字符串[重复]
【发布时间】:2013-12-21 01:42:24
【问题描述】:

我有一个类似

的字符串
                     address
                     424 G UT <br>

所以当我使用

      string trimstr= node5.InnerHtml.Trim();

这只会对齐第一行而不是第二行。
也打印在 html 中。如何使用 HTML 敏捷包删除 html 标记和空格。

【问题讨论】:

    标签: c# asp.net html-agility-pack


    【解决方案1】:

    用这个修改你的c#代码

    这里是html

    <div id="node5" runat="server">
                     address
                     424 G UT <br>
    </div>
    <div>
        <asp:Button ID="btmGetDivdetails" runat="server" Text="getDivdetails" 
            onclick="btmGetDivdetails_Click" />
    
    </div>
    

    这里点击按钮上的c#代码

    protected void btmGetDivdetails_Click(object sender, EventArgs e)
    {
      string trimstr = node5.InnerHtml;
      trimstr = trimstr.Replace("\r\n<br>", "").Replace("\r", "").Replace("\n", "").Trim();
      if (trimstr.Contains("<br>"))
      {
          trimstr =  trimstr.Replace("<br>", "");
      }
      List<char> result = trimstr.ToList();
      result.RemoveAll(c => c == ' ');
      string finalresult = new string(result.ToArray());
    }
    

    现在最终结果是“address424GUT”

    【讨论】:

    • 嘿,伙计们,我在 c# 中使用了 trim 函数,我得到了像这样的随机格式的文本(很多空格和换行符)。
    猜你喜欢
    • 2010-10-13
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多