Remove unwanted characters from text string

Solved a problem in an elegant manner and want to show off your code? Know a hard-to-find feature? Post it here for the benefit of others. Questions don't belong here.
Post Reply
steve
Posts: 384
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 77 times

Remove unwanted characters from text string

Post by steve » Tue Jun 11, 2013 9:52 am

Various scenarios call for a text string with only certain characters allowed. e.g A-Z a-z 0-9
whilst you can filter the allowed values of a field on a screen by right clicking on the field and select "Format...", sometimes you wish to remove unwanted charaters from an existing text field for use in e.g. an export routine.
You can use a maths field to format an existing alpha field into the correct format for your export, leaving your original alpha field intact.

Code: Select all

#initialise variable for holding output
v-out = "" 
#retrieve field that needs checking
v-in = {Accident Description.Text} 
#how many characters in it
    v-in-len = LENGTH( v-in )
#loop over each character, if the character is not a-z or A-Z  then replace it with a space 
    i = 0 
    WHILE i < v-in-len DO 
        i = i + 1 
#get one character at a time
        v-char = SPLIT( v-in , i , 1 ) 
#convert into ascii code
        v-asc = ASC( v-char ) 
#IF A-Z then do nothing
        IF v-asc >= 65 AND v-asc <= 90 THEN 
            #A-Z - do nothing
#if a-z then do nothing
        ELSEIF v-asc >= 97 AND v-asc <= 122 THEN 
            #a-z - do nothing
        ELSE 
#not a valid char. replace with space 
            v-char = " " 
        END 
#build output 
        v-out = TEXT( v-out ) + v-char 
    END 
result = v-out

Code Killer
Posts: 8
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 8 times

Re: Remove unwanted characters from text string

Post by Code Killer » Fri Apr 01, 2016 2:41 pm

Hi Steve,

I'm trying to use this in order to replace the space in the handler name with a dot.

I've read through the code and in my mind it should work however the final message box returns only the last character of the handler name. :?

Any idea where I'm going wrong? :oops:

Thanks

Code: Select all

STATUS( {case.key} ) 
v-status = return-value 
# 
#initialise variable for holding output 
v-handler = "" 
#retrieve field that needs checking 
v-in = v-status 
MESSAGE( v-status ) 

#how many characters in it 
v-in-len = LENGTH( v-in ) 
MESSAGE( v-in-len ) 

#loop over each character, if the character is not a-z or A-Z  then replace it with a space 
i = 0 
WHILE i < v-in-len DO 
    i = i + 1 
    #get one character at a time 
    v-char = SPLIT( v-in , i , 1 ) 
    #convert into ascii code 
    v-asc = ASC( v-char ) 
    #IF A-Z then do nothing 
    IF v-asc >= 65 AND v-asc <= 90 THEN 
        #A-Z - do nothing 
        #if a-z then do nothing  
    ELSEIF v-asc >= 97 AND v-asc <= 122 THEN 
        #a-z - do nothing  
    ELSE 
        #not a valid char. replace with dot 
          v-char = "." 
    END 
    #build output 
    v-handler = TEXT( v-out ) + v-char 
END 
MESSAGE( v-handler ) 

steve
Posts: 384
Joined: Wed Nov 30, 2011 10:20 pm
Been thanked: 77 times

Re: Remove unwanted characters from text string

Post by steve » Fri Apr 01, 2016 2:53 pm

looks like you have replaced the output variable v-out with v-handler in some places but not others

Code: Select all

    #build output 
    v-handler = TEXT( v-out ) + v-char 
should be

Code: Select all

#build output 
    v-handler = TEXT( v-handler ) + v-char


and for completeness, convert the single character into a TEXT

Code: Select all

    #build output 
    v-handler = TEXT( v-out ) + TEXT(v-char) 

Code Killer
Posts: 8
Joined: Thu Oct 29, 2015 10:04 am
Location: Kent
Has thanked: 8 times

Re: Remove unwanted characters from text string

Post by Code Killer » Fri Apr 01, 2016 3:17 pm

Such a rookie error, damn Fridays haha :lol:
Image

Thanks for the help, it works perfectly :D

Code: Select all

STATUS( {case.key} ) 
v-status = return-value 
# 
#initialise variable for holding output 
v-handler = "" 
#retrieve field that needs checking 
v-in = v-status 
MESSAGE( v-status ) 
#how many characters in it 
v-in-len = LENGTH( v-in ) 
MESSAGE( v-in-len ) 
#loop over each character, if the character is not a-z or A-Z  then replace it with a space 
i = 0 
WHILE i < v-in-len DO 
    i = i + 1 
    #get one character at a time 
    v-char = SPLIT( v-in , i , 1 ) 
    #convert into ascii code 
    v-asc = ASC( v-char ) 
    #IF A-Z then do nothing 
    IF v-asc >= 65 AND v-asc <= 90 THEN 
        #A-Z - do nothing 
        #if a-z then do nothing  
    ELSEIF v-asc >= 97 AND v-asc <= 122 THEN 
        #a-z - do nothing  
    ELSE 
        #not a valid char. replace with dot 
        v-char = "." 
    END 
    #build output 
    v-handler = TEXT( v-handler ) + TEXT( v-char ) 
END 
MESSAGE( v-handler ) 

Post Reply