簡體   English   中英

C#將2D數組轉換為html表

[英]C# Convert 2D array to html table

我有一個二維字符串數組,我想將其轉換為html表。 當我運行下面的代碼時,由於數組的長度很大,因此要花費很多時間。

  string data = string.Empty;
        string table = string.Empty;

        #region new code
        data += "<div class=\"tab-content\">";


        table = " <table class=\"table data-table\">";
        table += "<tbody>";
        for (int row = 1; row < Data.GetLength(0); row++)
        {
            table += "<tr>";
            for (int column = 1; column < Data.GetLength(1); column++)
            {
                try
                {
                    table += "<td>" + Data[row, column].ToString() + "</td>";
                }
                catch
                {
                    table += "<td></td>";
                }
            }
            table += "</tr>";
        }
        table += "</tbody>";


        table += "</table>";

嘗試使用StringBuilder 這樣,您將創建多個字符串實例,因為字符串是不可變的

根據try { }打擊的頻率,我將嘗試事先診斷索引是否合法。 發生很多情況時,Try-Catch 確實非常昂貴。

[編輯:]等等,它不應該是索引,您正在檢索邊界。 這是Try-Catch空檢查替代品嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM