Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@p0onage 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