【问题标题】:Formatting of full address using c# code使用 c# 代码格式化完整地址
【发布时间】:2017-04-01 21:39:40
【问题描述】:

我已经编写了以下几行用于格式化地址的代码

         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,城市,州,别针”

如果其中任何一个丢失,假设物理地址 2,那么地址应该是

“物理地址1,城市,州,别针”

如果表格中缺少城市,那么它应该像

“物理地址1,物理地址2,状态,引脚”

如果 Physical Address2、City、State、Pin 缺失,则地址应为

“Physical Address1”,目前上面是放置,在这种情况下。我无法处理这个。请帮忙!!!

此外,如果没有可用的文本,则文本应类似于“不可用”

【问题讨论】:

    标签: c# c#-4.0 c#-3.0


    【解决方案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(", ");
    

    【讨论】:

      【解决方案2】:

      字符串地址1 = 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>
      

      【讨论】:

        【解决方案3】:

        我想你想写得更干净。创建List&lt;string&gt;

        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)));
        

        【讨论】:

        • 感谢您的出色回答,但它在 list.toArray() 处抛出错误
        • @Nida 你有使用 System.Linq; ?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 2017-05-04
        • 1970-01-01
        • 2017-04-15
        相关资源
        最近更新 更多