0

I need to format addres in one string. Now I have properties like:

public string Street { get; set; }
public string StreetNumber { get; set; }
public string FlatNumber { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }

Now I have

String.Format("{0} {1} / {2} {3} {4}", model.Address.Street, model.Address.StreetNumber, model.Address.FlatNumber, data.Address.PostalCode, data.Address.City);
  1. Postal code is in format "xxxxx" (x is number). I want have postal code in format "xx-xxx".
  2. There is no always Flat number, so how can I hide flat number and character '/' if flat number is empty string ?
mskuratowski
  • 4,014
  • 15
  • 58
  • 109

3 Answers3

3

I would use {0:00-000} format for postal code and I would simply use extra variable to prepare flat part as empty string or its value with the slash.

public class Address
{
  public string Street { get; set; }
  public string StreetNumber { get; set; }
  public string FlatNumber { get; set; }
  public string PostalCode { get; set; }
  public string City { get; set; }

  public override string ToString()
  {
    string flatNumberStr = !string.IsNullOrEmpty(FlatNumber) ? " / " + FlatNumber : "";
    return string.Format("{0} {1}{2} {3:00-000} {4}", Street, StreetNumber, flatNumberStr, int.Parse(PostalCode), City);

  }
}

private static void Main(string[] args)
{
  Address addr1 = new Address()
  {
    Street = "Some Street",
    StreetNumber = "123",
    FlatNumber = "F3",
    PostalCode = "54897",
    City = "Big City"
  };

  Address addr2 = new Address()
  {
    Street = "Other Street",
    StreetNumber = "12B",
    PostalCode = "06816",
    City = "Smaller City"
  };

  Console.WriteLine(addr1.ToString());
  Console.WriteLine(addr2.ToString());
}

This code output is:

Some Street 123 / F3 54-897 Big City

Other Street 12B 06-816 Smaller City

Now note that the code only works if PostalCode is a number. So do check that before you call ToString method.

If you are not sure about its actual value, but you are sure that there will be 5 "digits", say "1234A" would be a valid postal code, then use

{3:##-###} 

instead of that

{3:00-000}
Community
  • 1
  • 1
Wapac
  • 4,058
  • 2
  • 20
  • 33
2

You can do this:

String.Format("{0} {1} {2} {3}-{4} {5}", 
    model.Address.Street, 
    model.Address.StreetNumber, 
    (!string.IsNullOrEmpty(model.Address.FlatNumber ? '/ ' + model.Address.FlatNumber : ""), 
    data.Address.PostalCode.Substring(0, 2), 
    data.Address.PostalCode.Substring(2), 
    data.Address.City);

A couple of things to point out:

  1. I removed the slash / from your format string and added a statement using a ternary operator

    (!string.IsNullOrEmpty(model.Address.FlatNumber ? "/ " + model.Address.FlatNumber : "")

This will check if the FlatNumber is null and, if not, use / followed by the FlatNumber or, if so, use just an empty string.

  1. I added an additional index to your format, i.e. {3}-{4}, for the postal code. Then the associated statements will extract portions of the postal code to go before and after the dash -:

    data.Address.PostalCode.Substring(0, 2) //before the dash data.Address.PostalCode.Substring(2) //after the dash

rory.ap
  • 34,009
  • 10
  • 83
  • 174
0

Try this

        private string postalCode = "";
        public string PostalCode
        {
            get{return postalCode.Substring(0,2) + "-" + PostalCode.Substring(2);}
            set { postalCode = value.Replace("-",""); }
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20