且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用String.Format格式化地址

更新时间:2023-11-10 09:14:28

String.Format如果某些值不返回空字符串为空.例如:
String.Format won''t return an empty string if some of the values are null. For example:
string val1 = null;
string val2 = "Something";

string.Format("Test: {0}/{1}", val1, val2);


给出Test: /Something
所以也许您还有其他问题.使用调试器尝试隔离返回字符串为空的部分.


Gives Test: /Something
So perhaps you have some other problem. Using the debugger try to isolate the part where the return string is getting empty.


使用如下代码:

Use something like this:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
void AddLine(string value) {
   if (!string.IsNullOrEmpty(value))
      sb.AppendLine(value); 
}
void AddLines(string[] values) {
   foreach(value in values)
      AddLine(value); 
}

//...

AddLines(new string[] {street, city, country, phone, email, }); // :-)



—SA



—SA


尝试一下-

Try This--

string street = "AAA"; 
string city = string.Empty;
string country = "SSS";
string FullAddress=string.Format("{0}{1}{2}{3}{4}",street ,(string.IsNullorEmpty(street)?"":Enviornment.NewLine),city,(string.IsNullorEmpty(city)?"":Enviornment.NewLine),country)



--Pankaj



--Pankaj