2

I want to format an address. Here is my code:

address = String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
                        postalAddress.Line1,
                        postalAddress.Line2,
                        postalAddress.Line3,
                        postalAddress.Line4,
                        postalAddress.Suburb,
                        postalAddress.StateCode,
                        postalAddress.Pcode);

Unfortunately this produces 116 Knox St, , , , Watson, ACT, 2602 when Line2, Line3, Line4 are null. How can I handle the nulls to get a results like 116 Knox St, Watson, ACT, 2602?

3
  • 1
    StringBuilder + if ? Commented Dec 12, 2013 at 0:26
  • Can you please elaborate? Commented Dec 12, 2013 at 0:27
  • 5
    do you know what the StringBuilder is? How about if? Commented Dec 12, 2013 at 0:27

4 Answers 4

12

Looks like this accomplishes your purpose much more concisely.

string[] data = new[] { 
    postalAddress.Line1, 
    postalAddress.Line2, 
    postalAddress.Line3, 
    postalAddress.Line4, 
    postalAddress.Suburb, 
    postalAddress.StateCode, 
    postalAddress.Pcode 
};

string address = string.Join(", ", 
                             data.Where(e => !string.IsNullOrWhiteSpace(e));
Sign up to request clarification or add additional context in comments.

Comments

1

Here you go. This is not going to be very efficient but should work ;)

StringBuilder sb = new StringBuilder();
List<string> addressParts = new List<string> { postalAddress.Line1, postalAddress.Line2, postalAddress.Line3, postalAddress.Line4, postalAddress.Suburb, postalAddress.StateCode, postalAddress.Pcode };
addressParts.ForEach(x => sb.Append(String.IsNullOrEmpty(x) ? "" : ", " + x));
string address = sb.ToString().Trim(',');

Comments

1

I'd recommend McAden solution, it's elegant and solves your problem nicely. Stated below is one option on how to use the StringBuilder, it's very verbose compared to the others.

As stated in the comments you can use the StringBuilder and a series of if statements. Thsi code can be cleaned up nicely but to let you play with it and understand what is going on, here is an example:

var postalAddress = new {
    Line1 = "116 Knox St",
    Line2 = "",
    Line3 = "",
    Line4 = "",
    Suburb = "Watson",
    StateCode = "ACT",
    Pcode = "2602",
};
var builder = new StringBuilder();

builder.AppendFormat("{0}, ", postalAddress.Line1);

if(!string.IsNullOrEmpty(postalAddress.Line2))
{
    builder.AppendFormat("{0}, ", postalAddress.Line2);
}
if(!string.IsNullOrEmpty(postalAddress.Line3))
{
    builder.AppendFormat("{0}, ", postalAddress.Line3);
}
if(!string.IsNullOrEmpty(postalAddress.Line4))
{
    builder.AppendFormat("{0}, ", postalAddress.Line4);
}

builder.AppendFormat("{0}, ", postalAddress.Suburb);
builder.AppendFormat("{0}, ", postalAddress.StateCode);
builder.AppendFormat("{0}", postalAddress.Pcode);

var result = builder.ToString();

When you print the result variable you will now have the following result:

116 Knox St, Watson, ACT, 2602

Basically you can use AppendFormat to append the string in a formatted manner and you can experiment and play around with this until it behaves as you like, then you can refactor it to be a bunch less lines thane this!

Comments

0

What about a one liner Regex to remove the duplicate , and spaces.

address = String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
                        postalAddress.Line1,
                        postalAddress.Line2,
                        postalAddress.Line3,
                        postalAddress.Line4,
                        postalAddress.Suburb,
                        postalAddress.StateCode,
                        postalAddress.Pcode);

address = Regex.Replace(address, @"(,\s)+", @"$1");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.