我已编写以下代码行来格式化地址
string Address1 = ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString();
string Address2 = ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString();
string Address1C;
string Address2C;
if (Address1 != "")
Address1C = Address1 + ", ";
else
Address1C = Address1;
if (Address2 != "")
Address2C = Address2 + ", ";
else
Address2C = Address2;
lblAdderssX1.Text = Address1C + Address2C;
string City = ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString();
string CityC;
if (City != "")
CityC = City + ", ";
else
CityC = City;
string Pin = ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString();
string State = ds.Tables[0].Rows[0]["JurisdictionX"].ToString();
string StateC;
if (State != "")
StateC = State + ", ";
else
StateC = State;
// string CountryC = ds.Tables[0].Rows[0]["CountryX"].ToString();
lblAddressX2.Text = CityC + StateC + Pin;
"<asp:Label ID="lblAdderssX1" CssClass="ProfileLabel" runat="server"> </asp:Label>
<asp:Label ID="lblAddressX2" CssClass="ProfileLabel" runat="server"> </asp:Label>"
实际上我们希望格式应该像
&#34;物理地址1,物理地址2,城市,州,地区&#34;
如果其中任何一个丢失,请说物理地址2然后地址应为
&#34;物理地址1,城市,州,Pin&#34;
如果表中缺少City,那么它应该是
&#34;物理地址1,物理地址2,状态,引脚&#34;
如果缺少物理地址2,城市,州,Pin,那么地址应该是
&#34;物理地址1&#34;在这种情况下,目前上面是放置。我无法处理这个问题。请帮忙!!!
此外,如果没有任何内容,那么文本应该是&#34;不可用&#34;
答案 0 :(得分:2)
我想你想把它写得更干净。创建List<string>
List<string> address = new List<string>();
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString());
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString());
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString());
address.Add(ds.Tables[0].Rows[0]["JurisdictionX"].ToString());
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString());
string list = string.Join(", ", address.Where(x => !string.IsNullOrEmpty(x)));
答案 1 :(得分:1)
string Address1 = ds.Tables [0] .Rows [0] [“PhysicalAddressLine1”]。ToString();
string Address2 = ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString();
string City = ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString();
string State = ds.Tables[0].Rows[0]["JurisdictionX"].ToString();
string Pin = ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString();
string Address1C;
if(!string.IsNullOrEmpty(Address1))
Address1C=Address1;
if (!string.IsNullOrEmpty(Address2))
{
if (!string.IsNullOrEmpty(Address1C))
Address1C = ", "+ Address2 ;
else
Address1C=Address2;
}
if (!string.IsNullOrEmpty(City))
{
if (!string.IsNullOrEmpty(Address1C))
Address1C = ", "+ City ;
else
Address1C=City;
}
if (!string.IsNullOrEmpty(State))
{
if (!string.IsNullOrEmpty(Address1C))
Address1C = ", "+ State ;
else
Address1C=State;
}
if (!string.IsNullOrEmpty(Pin))
{
if (!string.IsNullOrEmpty(Address1C))
Address1C = ", "+ Pin ;
else
Address1C=Pin;
}
if(!string.IsNullOrEmpty(Address1C))
lblAdderssX1.Text = Address1C;
else
lblAdderssX1.Text = "Not Available";
//<asp:Label ID="lblAdderssX1" CssClass="ProfileLabel" runat="server"> </asp:Label>
答案 2 :(得分:1)
试试这个,
string address = string.Empty;
if ((ds.Tables[0].Rows[0]["PhysicalAddressLine1"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString()))
address += ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString() + ", ";
if ((ds.Tables[0].Rows[0]["PhysicalAddressLine2"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString()))
address += ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString() + ", ";
if ((ds.Tables[0].Rows[0]["PhysicalAddressCity"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString()))
address += ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString() + ", ";
if ((ds.Tables[0].Rows[0]["JurisdictionX"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["JurisdictionX"].ToString()))
address += ds.Tables[0].Rows[0]["JurisdictionX"].ToString() + ", ";
if ((ds.Tables[0].Rows[0]["PhysicalAddressPin"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString()))
address += ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString() + ", ";
address = address.TrimEnd(", ");