0

I need to trim a string , say abc+cd+ze:::123:::12+abcd , given 123 , I need to extract ze:::123:::12.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
mac100
  • 137
  • 2
  • 13
  • Hi! SUBSTRING is the obvious command. You presumably need to find the 123 and then determine how far before and after you need. You haven't given enough information to be able to help you with that - what are the rules? Are you looking for everything between two +s? – Screwtape Mar 24 '16 at 08:45
  • Yes everything substring given and between nearest two +s – mac100 Mar 24 '16 at 08:49

2 Answers2

1

While on the surface, substring is the obvious way, because you are looking for something between two delimiters, actually ENTRY is easier. However, that only works if you can guarantee that the string you are looking for does not contain the delimiter. Progress cannot decode quoted or otherwise escaped delimiters.

This seems to work for me:

DEF VAR testStr AS CHAR INITIAL "abc+cd+ze:::123:::12+abcd".
DEF VAR matchStr AS CHAR INITIAL "123".
DEF VAR outStr AS CHAR.
DEF VAR delim AS CHAR INITIAL "+".

DEF VAR i AS INT.

DO i = 1 TO NUM-ENTRIES( testStr, delim ): 
  IF ENTRY( i, testStr, delim ) MATCHES "*" + matchStr + "*" THEN DO:
    outStr = ENTRY( i, testStr, delim ).
    LEAVE.
  END.
END.

DISPLAY outStr.
Screwtape
  • 1,337
  • 2
  • 12
  • 27
  • When I saw the question, I thought of making OpenEdge seeing it as a delimited list too (with + as a delimiter). But I´d have a temp-table with a position field and the entry, and in each iteration of DO i = 1 to NUM-ENTRIES... I´d create a record, make i the position and the result of the entry function the entry value. This way, you´d store the whole list in a better way to search and use. Just my 2 cents – bupereira Mar 24 '16 at 13:15
0

As Screwtape said it's quite easy to do this using ENTRY.

If you for some reason want to use the INDEX and search positions you can do like this. R-INDEX will help you - searching the string from right to left instead of left to right.

This example will have issues if you for instance have multiple entries that match your searching string. In that case it will return the leftmost matching entry.

DEFINE VARIABLE cString  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cSearch  AS CHARACTER   NO-UNDO.
DEFINE VARIABLE cResult  AS CHARACTER   NO-UNDO.

DEFINE VARIABLE iPosition  AS INTEGER     NO-UNDO.
DEFINE VARIABLE iLeftPlus  AS INTEGER     NO-UNDO.
DEFINE VARIABLE iRightPlus AS INTEGER     NO-UNDO.
DEFINE VARIABLE iLength    AS INTEGER     NO-UNDO.

/* This is the string we're searching in */
cString = "abc+cd+ze:::123:::12+abcd".
/* This is what we're searching for */
cSearch = "123".

/* Get a starting position */
iPosition = INDEX(cString, cSearch).

/* Start at starting position and look right-to-left for a plus sign */
/* Add 1 since we don't want the plus sign */
iLeftPlus  = R-INDEX(cString, "+", iPosition) + 1.

/* Start at starting position and look left-to-right for a plus sign */
iRightPlus = INDEX(cString, "+", iPosition).

/* If there isn't a rightmost + */
IF iRightPlus = 0 THEN
    iRightPlus = LENGTH(cString).


/* Calculate the length of the result string */
iLength = iRightPlus - iLeftPlus.

/* Use substring to create resulting string */

cResult = SUBSTRING(cString, iLeftPlus, iLength).

MESSAGE cResult VIEW-AS ALERT-BOX INFORMATION.
Jensd
  • 7,886
  • 2
  • 28
  • 37