Skip to main content
Jennell_McIntire
Employee
Employee

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.

FirstName.png

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.

Purge script.png

Once I run the script the names look like this:

FirstName Clean.png

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.

ProductCode.png

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.

Keep script.png

The end result looks like the image below.  The product codes are now all numeric and the letters have been removed.

ProductCode Clean.png

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.

 

Thanks,

Jennell

19 Comments
prieper
Master II
Master II

I am using a similar solution in reading textfiles, which are created by different persons in different countries. You encounter a different presentation of numbers ("," as decimal or thousand-separator, leading or trailing "-" or brackets to display negative numbers and currencies, also either trailing or leading in the same cell.

Thanks

Peter

9,123 Views
cksuperlatif
Contributor III
Contributor III

Thank you much I used it despite regulare expression

0 Likes
9,123 Views
paul_scotchford
Specialist
Specialist

Nice hint, thanks for posting.

0 Likes
9,123 Views
NareshGuntur
Partner - Specialist
Partner - Specialist

Thanks Jennell. Simple and important functions which people rarely use.

0 Likes
9,123 Views
ishanbhatt
Creator II
Creator II

Thanks a lot Jennell. It's help a lot.:)

0 Likes
9,123 Views
Not applicable

Thanks, Nice one

0 Likes
9,123 Views
tiago_hubner
Partner - Contributor III
Partner - Contributor III

Nice, thanks.

6,892 Views
Chanty4u
MVP
MVP

very nice,,

0 Likes
6,892 Views
HirisH_V7
Master
Master

Thanks a lot .

0 Likes
6,892 Views
Anil_Babu_Samineni

Wav, Really nice and simple

0 Likes
6,892 Views