簡體   English   中英

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

其實我們希望格式應該像

“物理地址1,物理地址2,城市,州,Pin”

如果缺少任何一個,例如“物理地址2”,則地址應為

“物理地址1,城市,州,Pin”

如果表中缺少城市,則應為

“物理地址1,物理地址2,狀態,引腳”

如果缺少物理地址2,城市,州,PIN,則該地址應類似於

在這種情況下,“ Physical Address1”和當前的內容正在放置。 我無法處理。 請幫忙 !!!

另外,如果沒有可用的內容,則文本應為“不可用”

我想你想寫得更干凈。 創建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)));

字符串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>

嘗試這個,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM