Click here to Skip to main content
15,915,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class with properties Street, PoBox, City. Some can have empty values from these. I want to give a method to take full address from those methods like
Street,
PoBox,
City.

How can I create this by using string.format. When I'm using this, if any property has empty it gives empty line.

[edit]Minor corrections for readability[/edit]

My code is like this

C#
string street = "AAA";
string city = string.Empty;
string country = "SSS";

string add = string.Format("{0}\n{1}\n{2}",street, city, country);
Console.WriteLine(add);

Result:
AA

SSS

My problem is when city is empty, extra line comes between the street and county.
Posted
Updated 4-Apr-11 20:35pm
v3

String.Format won't return an empty string if some of the values are null. For example:
C#
string val1 = null;
string val2 = "Something";

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

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 2:38am    
Good point. The solution is obvious, so... a 5.
--SA
Michel [mjbohn] 5-Apr-11 3:00am    
This solution takes care of null value, but won't solve the empty row prob that Sanjeewabdissa was asking. Nonetheless a good answer
Sergey Alexandrovich Kryukov 5-Apr-11 3:22am    
Well... yes I agree. I added my Answer :-)
--SA
Sanyon_d 5-Apr-11 4:12am    
I got the answer,
fullAddress =string.Format("{0}\n{1}\n{2}",val1,val2,val3);
fullAddress = fullAddress.Replace("\n\n", "\n");
Use something like this:

C#
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
 
Share this answer
 
Comments
Olivier Levrey 5-Apr-11 3:50am    
Who downvoted this?? This answer is perfectly correct. My 5 SA.
Sergey Alexandrovich Kryukov 5-Apr-11 13:17pm    
Thank you Olivier.
--SA
Michel [mjbohn] 5-Apr-11 3:58am    
Who thinks this answer deserves a vote of 1? Just can't follow
my 5
Sergey Alexandrovich Kryukov 5-Apr-11 13:17pm    
Thank you very much. (This happens :=<)
--SA
Kim Togo 5-Apr-11 9:30am    
Good solution. I will borrow that :-)
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
 
Share this answer
 
Comments
Olivier Levrey 5-Apr-11 3:55am    
This will work as well but I don't really like copied/pasted code (you duplicated the IsNullOrEmpty and ?: test).
Voted 4.
Give StringBuilder a try

StringBuilder sb = new StringBuilder();

if (street != string.emty)
sb.AppendLine(street);

if (city != string.emty)
sb.AppendLine(city);

if (country != string.emty)
sb.AppendLine(country);

Console.WriteLite(sb.toString());
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 2:37am    
That will work, my 3.
Why 3? because you should not assume non-null.
--SA
you can also try it

C#
string street = "AAA";
            string city = string.Empty;
            string country = "SSS";
           
 string  add = street + "\n" + city + "\n" + country;
            string[] Adress = add.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in Adress)
            {
                Console.WriteLine(str);
            }

output :-
AAA
SSS
 
Share this answer
 
Comments
Olivier Levrey 5-Apr-11 3:52am    
It looks a litte bit more complicated but will work.
And it is better to use Environment.NewLine instead of hardcoded '\n'.
Voted 4.
[Moved to Question]
OP should not post this text as Answer, to be removed. Next time, use "Improve answer", "Add comment" or reply to existing comment.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900