Deleting all special characters from a string in progress 4GL

笑着哭i 提交于 2019-12-10 10:33:43

问题


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


回答1:


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.


来源:https://stackoverflow.com/questions/24525858/deleting-all-special-characters-from-a-string-in-progress-4gl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!