在我的C#表单中,我有地址字段,因为我需要填写已经将门禁,街道,区域,位置分别存储在Access数据库中的地址。我使用字符串连接来合并完整地址。
object row = CBL_Customer_Name.Properties.GetDataSourceRowByKeyValue(CBL_Customer_Name.EditValue) as object;
string getblocation = (row as DataRowView)["BLocation"].ToString();
string getbcity = (row as DataRowView)["BCity"].ToString();
string getbstate = (row as DataRowView)["BState"].ToString();
string getbcountry = (row as DataRowView)["BCountry"].ToString();
TXE_Invoice_Address.Text = (row as DataRowView)["BFlatNo"].ToString() + ", " + (row as DataRowView)["BPremises"].ToString() + "," + System.Environment.NewLine + (row as DataRowView)["BStreet"].ToString() + ", " + getloction(getblocation) + "," + System.Environment.NewLine + (row as DataRowView)["BArea"].ToString() + ", " + getcity(getbcity) + "," + System.Environment.NewLine + getstate(getbstate) + ", " + getcountry(getbcountry) + ".";
如果用户输入完整地址,则上述代码没有问题。如果他没有输入位置或其他字段,那么我会在行尾获得 , ,
或空格。
怎么解决这个?如果用户也留下1或2个字段,我在地址栏中需要一个完美的地址。
答案 0 :(得分:1)
使用.replace()
替换自定义文字的空值。
示例强>
TXE_Invoice_Address.Text = TXE_Invoice_Address.Text.Replace(" ,,",",")
此代码将删除可能导致奇怪地址格式的所有, ,
。
答案 1 :(得分:1)
试试这个:var newAddress = string.Join(", ",TXE_Invoice_Address.Text.Split(',').Where(x => !string.IsNullOrWhiteSpace(x)));
这将消除所有空格并重新加入新的字符串。