c# split multi-line postal address
I have a multi-line postal address (string PostalAddressString) like this:
Address1:12 Garden Street
Address2:
Suburb:Abc
PostCode:123
State:BC
Country:ABCDE
In order to separate details and access them individually i need to do this:
NameValueCollection lines = new NameValueCollection();
string[] TempArray = PostalAddressString.Split(Environment.NewLine.ToCharArray());
if (TempArray.Count() > 5 || TempArray[0].ToLower() != "n/a")
{
foreach (string line in TempArray)
{
string[] parts = line.Split(':');
if (line.Length > 0)
lines.Add(parts[0].Trim(), parts[1].Trim());
}
string PostalAddressLine1String = lines.Get("Address Line 1");
string PostalAddressLine2String = lines.Get("Address Line 2");
string PostalSuburbString = lines.Get("Suburb");
string PostalPostCodeString = lines.Get("PostCode");
string PostalStateString = lines.Get("State");
string PostalCountryString = lines.Get("Country");
}
No comments:
Post a Comment