簡體   English   中英

我如何從表中讀取字符串值,並追加到單個字符串

[英]How can i read string values from a table , and append to a single string

我正在從表中讀取所有字符串值,並如下所示將其添加到數組中。

da2.Fill(ds, "DNAiusTwitter_Table");
int counts = ds.Tables[0].Rows.Count;
for (int i = 0; i < counts; i++)
{
    names[i, 0] = ds.Tables[0].Rows[i][3].ToString();
}

如何獲取字符串append = 'name1','name2','name3','name4'; 我如何將這些值傳遞給以下代碼:

for (int i = 0; i < 1; i++)
{
    HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script");
    scriptTagLinks.Attributes["type"] = "text/javascript";
    string scrip1 = "$(function () {$(\"[src='" + names[i, 0] + "']\"" + ").pinit();});";
    var scrip = "var tweetUsers=['" +append  + "'," + "'" + splitted[3]+"']";
    scriptTagLinks.InnerHtml = scrip;

    this.Controls.Add(scriptTagLinks);
}

如果以后在代碼中不需要數組,則可以使用StringBuilder(System.Text名稱空間),如果表的大小發生變化,則它具有更好的內存分配。

da2.Fill(ds, "DNAiusTwitter_Table");
int counts = ds.Tables[0].Rows.Count;
StringBuilder appendString = new StringBuilder();
for (int i = 0; i < counts; i++)
{
    appendString.AppendFormat("{0},", ds.Tables[0].Rows[i][3].ToString());
}

這會將所有數據添加到構建器中,然后在第二個代碼片段中,執行以下操作以將構建器轉換為字符串,並在末尾去除附加的逗號。 我也不認為您需要第二個代碼段中的for循環(通過1?)還是for循環中的1是一個錯字?

var scrip = "var tweetUsers=['" + appendString.ToString().TrimEnd(new char[] { ',' }) + "'," + "'" + splitted[3]+"']";

使用string.join()方法

string.Join(your_array, ",")

暫無
暫無

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

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