简体   繁体   中英

Most efficient way to parse delimited into html table c#

I've got the following delimited string with pairs:

1,5|2,5|3,5

I want to create a table as follows:

< table>
     < tr>< td>1< /td>< td>5< /td>< /tr>
     < tr>< td>2< /td>< td>5< /td>< /tr>
     < tr>< td>3< /td>< td>5< /td>< /tr>
     < /table>

What's the most efficient way in C#?

Parse the string (simple splitting should be enough) and I'd suggest using the .NET XML classes (or Html Agility Pack for the purists out there) to generate the table. Might be overkill vs building up the string manually especially for simple data but it is less verbose and should be easier to extend later.

Using LINQ to XML:

var str = "1,5|2,5|3,5";
var table =
    new XElement("table",
        str.Split('|')
           .Select(pair =>
               new XElement("tr",
                   pair.Split(',')
                       .Select(num => new XElement("td", num))
               )
           )
    ).ToString();

Yields the string:

<table>
  <tr>
    <td>1</td>
    <td>5</td>
  </tr>
  <tr>
    <td>2</td>
    <td>5</td>
  </tr>
  <tr>
    <td>3</td>
    <td>5</td>
  </tr>
</table>

Version 1: Straight-forward

String html = "<table>";
Array.ForEach<String>("1,5|2,5|3,5".Split('|'),r =>
{
  html += "<tr>";
  Array.ForEach(r.Split(','),c =>
  {
    html += String.Format("<td>{0}</td>", c);
  });
  html += "</tr>";
});
html += "</table>";

Untested, but something of the sort? I take it back, battle tested and working.

Version two, less the delegate:

String html = "<table>";
foreach (String r in "1,5|2,5|3,5".Split('|'))
{
  html += "<tr>";
  foreach (String c in r.Split(','))
    html += String.Format("<td>{0}</td>", c);
  html += "</tr>";
}
html += "</table>";

Both versions in a working demo.

And Another version which includes StringBuilder

If you search for efficient way, then you shouldn't use string concat, use StringBuilder instead:

    private static string ToTable(string input)
    {
        var result = new StringBuilder(input.Length * 2);
        result.AppendLine("<table>");
        foreach (var row in input.Split('|'))
        {
            result.Append("<tr>");
            foreach (var cell in row.Split(','))
                result.AppendFormat("<td>{0}</td>", cell);
            result.AppendLine("/<tr>");
        }
        result.AppendLine("</table>");
        return result.ToString();
    }

Create a IList from your collection as described above using the String.Split method in the code behind and use the native DataList UI Control , bind the datasource to the control and set the DataSource property of the control to your List.

 <asp:DataList ID="YourDataList" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Horizontal" runat="server">
     <ItemTemplate>
        <%# Eval("value") %> 
     </ItemTemplate>
 </asp:DataList>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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