Page 1 of 1
Stripping Non Printing Characters
Posted: Wed Aug 04, 2004 7:45 am
by ShaneMuir
Hi everybody
I am sure this is quite an easy one for all of you but here goes anyway:
I wish to strip all the non printing characters from an input string. I am aware of the routine
Oconv(expression, MCP) but I do not wish to replace the offending character with a period, rather a character of my choice.
I know it can be done as it I have seen it done before, but I cannot remember exactly how.
Thank you in Advance
Shane
Posted: Wed Aug 04, 2004 9:09 am
by crouse
ereplace(Oconv(StringWithNonPrintingCharacters, "MCP"),".",MyNewCharacterOfChoice,0,1) will work.
If you might have periods in your string to begin with, do an inner ereplace to change them to a unique character first, then another outer ereplace to change them back to period after converting the non-printing characters.
For execution speed and reusability, do this all in one line and create a transform for it.
For maintainablity and reusability, do it in several lines in a routine.
-Craig
Posted: Wed Aug 04, 2004 10:08 pm
by ray.wurlod
Welcome aboard! Long way from Brisbane!
About the only way to strip all non-printing characters is to create a Routine that processes each character.
Oconv with "MCA", "MC/A", "MCN", "MC/N" and "MCP" are probably too specific for your needs.
Code: Select all
FUNCTION RemoveNonPrintingCharacters(TheString)
Ans = ""
CharCount = Len(TheString)
For cpos = 1 To CharCount
TheChar = Seq(TheString[cpos,1])
If TheChar >= 32 And TheChar <= 127
Then
Ans := TheChar
End
Next cpos
RETURN(Ans)
If you're working with a non-ASCII character set, such as a European character set that includes accented characters, your definition of "printable" may need to change.
If NLS is enabled, prefer UniSeq to Seq.
Posted: Thu Aug 05, 2004 3:30 am
by ShaneMuir
Thanks for that guys. Most appreciated.
Shane
PS: Yes Ray I am a long way from Brisbane :D
Alternat Routine Code
Posted: Thu Aug 05, 2004 4:08 am
by hassers
Very similar to Rays Code, do initial check for null string being received and stip trailing spaces:
Input variable SourceString
Ans = ''
lenvar= len(trimb(SourceString))
if lenvar < 1 then Ans = ' '
For i = 1 to lenvar step 1
char1 = SourceString[i,1]
nchar = seq(char1)
if nchar <32 or nchar>127 then
Ans := ' '
End Else
Ans := char1
End
Next i
Posted: Thu Aug 05, 2004 5:02 am
by ray.wurlod
Trailing spaces may be valid data.
NULL would be OK in my routine, because Len(@NULL) is zero (go on, try it). However, my routine would return "" in that case, which may not be the desired result.
If you're going to write a completely bullet-proof routine, you need also to check that the incoming arguments are not in an unassigned state. Depending on how you like to cast the logic, use either the Assigned or the UnAssigned function to test.