使用c#代码格式化完整地址

本文关键字:地址 格式化 代码 使用 | 更新日期: 2023-09-27 18:18:27

我编写了以下几行代码来格式化地址

         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>"

实际上我们希望格式应该是

"物理地址s1,物理地址s2,城市,州,Pin"

如果其中任何一个缺失比如物理地址s2那么地址应该是

"物理地址1,城市,州,Pin"

如果表格中没有City,那么应该是

"Physical Address1, Physical Address2, State, Pin"

如果Physical Address2, City, State, Pin丢失,那么地址应该像

"Physical Address1",在本例中,目前上面正在放置。我处理不了这事。请帮忙!!

如果没有可用的,那么文本应该是"Not available "

使用c#代码格式化完整地址

我猜您想写得更清楚一些。创建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)));

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>

try this,

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(", ");