gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:33:04 25 4
gpt4 key购买 nike

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

         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”,目前上面是 placing ,在这种情况下。我无法处理这个。请帮忙!!!

此外,如果没有可用的文本,则文本应显示为“不可用”

最佳答案

我想你想把它写得更干净。创建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)));

关于c# - 使用 C# 代码格式化完整地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40670269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com