[英]How to get rid of unwanted spaces after using ToString() in C#?
我不確定這可能是Session而不是ToString()的問題。
我有兩個.aspx頁,我想將數據表中的IP地址從一頁傳遞到另一頁。 當我這樣做時,會添加不需要的空格。 代碼的簡單版本是這樣的:
.aspx第一頁
int num = DropDownList1.SelectedIndex;
DataView tempDV = SqlDataSource2.Select(DataSourceSelectArguments.Empty) as DataView;
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2];
Response.Redirect("test.aspx");
test.aspx頁
string ipCamAdd = Session["camera"].ToString();
TextBox1.Text = "http://" + ipCamAdd + "/jpg/image.jpg?resolution=320x240";
我要打印的是
http ://ipadd/jpg/image.jpg?resolution=320x240
但是打印出來的是
http//ipaddress /jpg/image.jpg?resolution=320x240
我怎樣才能解決這個問題?
另外,我問了這個問題,希望有人能告訴我為什么也會發生這種情況。 對不起,這個錯誤。
嘗試這個:
string ipCamAdd = Session["camera"].Trim().ToString();
對於有效的關注,Session [“ camera”]可以為null,在您的代碼中添加以下功能
static string ToSafeString(string theVal) { string theAns; theAns = (theVal==null ? "" : theVal); return theAns; }
然后使用:
字符串ipCamAdd = Session [“ camera”]。ToSafeString()。Trim();
如果只想擺脫空格,可以使用string.Replace
:
TextBox1.Text = "http://" + (ipCamAdd ?? "").Replace(" ", "") + "/jpg/image.jpg?resolution=320x240";
設置會話之前,請先修剪結果。
Session["camera"] = tempDV.Table.Rows[num].ItemArray[2].Trim();
似乎在SQL中,如果將數據類型轉換為varchar
並重新輸入數據,則您的數據類型為char(*)
,將不會獲得任何其他空格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.