2

How can I delete all special characters from a string in Progress 4GL?

Jensd
  • 7,886
  • 2
  • 28
  • 37
user3796722
  • 21
  • 1
  • 1
  • 2

4 Answers4

7

I guess this depends on your definition of special characters.

You can remove ANY character with REPLACE. Simply set the to-string part of replace to blank ("").

Syntax:

REPLACE ( source-string , from-string , to-string )

Example:

DEFINE VARIABLE cOldString AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cNewString AS CHARACTER   NO-UNDO.

cOldString = "ABC123AACCC".

cNewString = REPLACE(cOldString, "A", "").

DISPLAY cNewString FORMAT "x(10)".

You can use REPLACE to remove a complete matching string. For example:

REPLACE("This is a text with HTML entity &", "&", "").

Handling "special characters" can be done in a number of ways. If you mean special "ASCII" characters like linefeed, bell and so on you can use REPLACE together with the CHR function.

Basic syntax (you could add some information about code pages as well but that's rarely needed) :

CHR( expression )

expression: An expression that yields an integer value that you want to convert to a character value. (ASCII numberic value).

So if you want to remove all Swedish letter Ö:s (ASCII 214) from a text you could do:

REPLACE("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ", "Ö", "").

or

REPLACE("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ", CHR(214), "").

Putting this together you could build an array of unwanted characters and remove all those in the string. For example:

FUNCTION cleanString RETURNS CHARACTER (INPUT pcString AS CHARACTER):
    DEFINE VARIABLE iUnwanted AS INTEGER     NO-UNDO EXTENT 3.
    DEFINE VARIABLE i         AS INTEGER     NO-UNDO.
    /* Remove all capital Swedish letters ÅÄÖ */
    iUnwanted[1] = 197.
    iUnwanted[2] = 196.
    iUnwanted[3] = 214.

    DO i = 1 TO EXTENT(iUnwanted):

        IF iUnwanted[i] <> 0 THEN DO:
            pcString = REPLACE(pcString, CHR(iUnwanted[i]), "").
        END.
    END.

    RETURN pcString.
END.

DEFINE VARIABLE cString AS CHARACTER   NO-UNDO INIT "AANÅÅÖÖBBCVCÄÄ".

DISPLAY cleanString(cString) FORMAT "x(10)".

Other functions that could be useful to look into:

  • SUBSTRING: Returns a part of a string. Can be used to modify it as well.
  • ASC: Like CHR but the other way around - displays ASCII value from a character).
  • INDEX: Returns the position of a character in a string.
  • R-INDEX: Like INDEX but searches right to left.
  • STRING: Converts a value of any data type into a character value.
Jensd
  • 7,886
  • 2
  • 28
  • 37
  • Thank you for your answers. I'd like to provide an example for you to help me: do: def var namecust as char. def var namecustnew as char. namecust = bryan & Jérôme-caron/pers. /*I want to display the variable by replacing namecust & Eo-/ space ie: */ namecustnew = BRYAN JEROME CARON PERS. MESSAGE namecustnew        VIEW-AS ALERT-BOX INFO BUTTONS OK. end. – user3796722 Jul 03 '14 at 15:33
  • newstring = REPLACE("Jérôme","é","e"). newstring = REPLACE(newstring,"ô","o"). – Jensd Jul 04 '14 at 05:40
  • Although that function is a whole lot cleaner as far as maintainability is concerned, one could also nest the REPLACE calls, like so: newstring = REPLACE(REPLACE('"Jérôme","é","e"), "ô","o"). – zr00 Feb 16 '18 at 21:40
0

This function will replace chars according to the current collation.

function Dia2Plain returns character (input icTxt as character):
  define variable ocTxt as character no-undo.
  define variable i as integer no-undo.
  define variable iAsc as integer no-undo.
  define variable cDia as character no-undo.
  define variable cPlain as character no-undo.

  assign ocTxt = icTxt.
  repeat i = 1 to length(ocTxt):
  assign cDia = substring(ocTxt,i,1)
         cPlain = "".
    if asc(cDia) > 127
    then do:
      repeat iAsc = 65 to 90: /* A..Z */
        if compare(cDia, "eq" , chr(iAsc), "case-sensitive")
        then assign cPlain = chr(iAsc).
      end.
      repeat iAsc = 97 to 122: /* a..z */
        if compare(cDia, "eq" , chr(iAsc), "case-sensitive")
        then assign cPlain = chr(iAsc).
      end.
      if cPlain <> ""
      then assign substring(ocTxt,i,1) = cPlain.
    end.
  end.
  return ocTxt.
end.

/* testing */
def var c as char init "ÄëÉÖìÇ".

disp c Dia2Plain(c).

def var i as int.
def var d as char.

repeat i = 128 to 256:
  assign c = chr(i) d = Dia2Plain(chr(i)).
  if asc(c) <> asc(d) then disp i c d.
end.
Erik
  • 1
0

This function will remove anything that is not a letter or number (adapt it as you wish).

/* remove any characters that are not numbers or letters */
FUNCTION alphanumeric RETURN CHARACTER
    (lch_string AS CHARACTER).
DEFINE VARIABLE lch_newstring AS CHARACTER   NO-UNDO.
DEFINE VARIABLE i AS INTEGER     NO-UNDO.

DO i = 1 TO LENGTH(lch_string):

    /* check to see if this is a number or letter */
    IF (ASC(SUBSTRING(lch_string,i,1)) GE ASC("1") 
        AND ASC(SUBSTRING(lch_string,i,1)) LE ASC("9"))
    OR (ASC(SUBSTRING(lch_string,i,1)) GE ASC("A") 
        AND ASC(SUBSTRING(lch_string,i,1)) LE ASC("Z"))
    OR (ASC(SUBSTRING(lch_string,i,1)) GE ASC("a") 
        AND ASC(SUBSTRING(lch_string,i,1)) LE ASC("z"))
    THEN
        /* only keep it if it is a number or letter */
        lch_newstring = lch_newstring + SUBSTRING(lch_string,i,1).
    
END.

RETURN lch_newstring.
END FUNCTION.
0

Or you can simply use regex

System.Text.RegularExpressions.Regex:Replace("Say,Hi!", "[^a-zA-Z0-9]","")
SimonD
  • 51
  • 6