[英]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.