Consultor Eletrônico



Kbase P10873: The TRIM functions return unexpected results
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   6/4/2009
Status: Verified

SYMPTOM(s):

TRIM function trims too many characters

The LEFT-TRIM function returns unexpected results

Trim chars in RIGHT-TRIM function doesn't use pattern matching

FACT(s) (Environment):

Progress 9.x

CHANGE:

The LEFT-TRIM function was used to remove a string containing multiple characters from another string

CAUSE:

The trim functions are not exact pattern matching functions. The purpose of the function are to remove concurrent matching occurrences of any of the specified characters until the point at which there are no longer any matches.

For example,

MESSAGE LEFT-TRIM("FIIBAR", "IF")
will return "BAR" because all occurrences of the letter "I" are removed then all occurrences of the letter "F" are removed.

CAUSE:

This is expected behavior because the <trim-chars> parameter of the TRIM, LEFT-TRIM & RIGHT-TRIM functions is not an exact pattern matching algorithm but rather a list of characters to trim and the function will continue to trim instances of those characters until it encounters a character that is not in the list.

FIX:

To remove a given string from the beginning of another string use code similar to the following:

DEFINE VARIABLE cDesc1 AS CHARACTER NO-UNDO INIT "IA_MAHASKA__SILO".
DEFINE VARIABLE cDesc2 AS CHARACTER NO-UNDO INIT "IA_MAHASKA__".
/* Result of INDEX function is 1 if the string we want to */
/* remove exists at the beginning of the string that we */
/* want to check. */

IF INDEX(cDesc1,cDesc2) = 1 THEN
ASSIGN cDesc1 = SUBSTRING(cDesc1,LENGTH(cDesc2) + 1, -1).

MESSAGE cDesc1 VIEW-AS ALERT-BOX.

To remove a given string from the end of another string use code similar to the following:

DEFINE VARIABLE cDesc1 AS CHARACTER NO-UNDO INIT "SILO__IA_MAHASKA".
DEFINE VARIABLE cDesc2 AS CHARACTER NO-UNDO INIT "__IA_MAHASKA".

DEFINE VARIABLE iPosition AS INTEGER NO-UNDO.
iPosition = R-INDEX(cDesc1,cDesc2).
cDesc1 = SUBSTRING(cDesc1,1,iPosition - 1).
MESSAGE cDesc1 VIEW-AS ALERT-BOX.

To remove all instances of a given string from another string simply use the REPLACE function:

DEFINE VARIABLE cDesc1 AS CHARACTER NO-UNDO INIT "__IA_MAHASKASILO__IA_MAHASKA".
DEFINE VARIABLE cDesc2 AS CHARACTER NO-UNDO INIT "__IA_MAHASKA".

cDesc1 = REPLACE(cDesc1,cDesc2,"").

MESSAGE cDesc1 VIEW-AS ALERT-BOX.