Skip to content

Instantly share code, notes, and snippets.

@naveed-ahmad-biz
Created November 19, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveed-ahmad-biz/44f5daca0b7ec741fdf4fafa819784cc to your computer and use it in GitHub Desktop.
Save naveed-ahmad-biz/44f5daca0b7ec741fdf4fafa819784cc to your computer and use it in GitHub Desktop.
public static string NormalizePostcode(string postcode)
{
//removes end and start spaces
postcode = postcode.Trim();
//removes in middle spaces
postcode = postcode.Replace(" ", "");
switch (postcode.Length) {
//add space after 2 characters if length is 5
case 5: temp_postcode = temp_postcode.Insert(2, " "); break;
//add space after 3 characters if length is 6
case 6: temp_postcode = temp_postcode.Insert(3, " "); break;
//add space after 4 characters if length is 7
case 7: temp_postcode = temp_postcode.Insert(4, " "); break;
default: break;
}
return postcode;
}
@p0onage
Copy link

p0onage commented Sep 22, 2020

You could just do

temp_postcode = temp_postcode.Insert(postcode.Length - 3, " ");

wonder if this works internationally though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment