Do you even need to delete or keep some characters in a string field? The PurgeChar and KeepChar functions allow you to purge and keep characters that are in a string. The PurgeChar function takes two parameters. The first is the string and the second is the character(s) that are to be removed from the string. The KeepChar function also takes two parameters but in this case the second parameter is the character(s) that are to be kept in the string. Let’s take a look at some examples.
Sometimes you may have a dataset that has garbage in it like in the FirstName field below.
In this case there are characters after each name that I do not need. In order to remove these characters from the field, I can use the PurgeChar function in my script (see below) to remove all the unwanted characters from the FirstName field.
Once I run the script the names look like this:
The KeepChar function works similar except in this function you indicate what characters you would like to keep. This may be helpful when you have field that includes a mix of numbers and letters but you only want to keep the numbers or the letters. In this example, I have a ProductCode field that has codes that are made up of numbers and letters but I only want the numeric data.
In my script, I can use the KeepPurge function and use the second parameter to list all the numbers since those are the characters I want to keep in the string.
The end result looks like the image below. The product codes are now all numeric and the letters have been removed.
There are a host of string functions that can be used to clean up or modify a string but when there are specific characters that you need to remove or keep, PurgeChar and KeepChar can be helpful and easy to add to your script or chart expression. These functions work well when you need to remove or keep ALL references to a character in a string.