使用 C# 代码格式化完整地址
Formatting of full address using c# code
我已经编写了以下代码行来格式化地址
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>"
实际上我们希望格式应该像
"Physical Address1, Physical Address2, City, State, Pin"
如果缺少其中任何一个,假设物理地址 2 那么地址应该是
"Physical Address1, City, State, Pin"
如果 table 中缺少城市,那么它应该像
"Physical Address1, Physical Address2, State, Pin"
如果缺少物理地址 2、城市、州、密码,则地址应类似于
"Physical Address1" 并且当前上面是 placing ,在这种情况下。我无法处理这个。请帮助!!!
此外,如果没有可用的文本,那么文本应该像 "Not Available"
我想你想把它写得更干净。创建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 = ds.Tables[0].行[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>
试试这个,
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(", ");
我已经编写了以下代码行来格式化地址
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>"
实际上我们希望格式应该像
"Physical Address1, Physical Address2, City, State, Pin"
如果缺少其中任何一个,假设物理地址 2 那么地址应该是
"Physical Address1, City, State, Pin"
如果 table 中缺少城市,那么它应该像
"Physical Address1, Physical Address2, State, Pin"
如果缺少物理地址 2、城市、州、密码,则地址应类似于
"Physical Address1" 并且当前上面是 placing ,在这种情况下。我无法处理这个。请帮助!!!
此外,如果没有可用的文本,那么文本应该像 "Not Available"
我想你想把它写得更干净。创建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 = ds.Tables[0].行[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>
试试这个,
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(", ");