Hello together,
Is there any way except using the INDEX() and SUBSTRING() function to split a string by another self defined string?
I'm searching for something like the explode function in PHP.
Yes you can do that using the Split method of Regular Expression. Example code below:
USING System.Text.RegularExpressions.Regex. DEFINE VARIABLE cSearch AS CHARACTER INIT "cProductNumer = '123' AND cProductName BEGINS 'PEN'" FORMAT "x(60)". DEFINE VARIABLE oRegExp AS CLASS Regex NO-UNDO. DEF VAR liLoop AS INT NO-UNDO. DEF VAR cResult AS CHAR NO-UNDO EXTENT. oRegExp = NEW RegEx("AND"). cResult = oRegExp:Split(cSearch). REPEAT liLoop = 1 TO EXTENT(cResult): DISP TRIM(cResult[liLoop]) FORMAT "x(60)". END.
|
Not really sorry. I forgot to say that the delimiter can have more than one character.
Here an Example:
DEFINE VARIABLE cSearch AS CHARACTER INIT "cProductNumer = '123' AND cProductName BEGINS 'PEN'".
I need the following:
productNumber = "cProductNumer = '123'".
prodcutName = "cProductName BEGINS 'PEN'".
Separatet by "AND".
There isn't a function that will do that out of the box, but you could replace 'AND' with "," in a workstring and then use entry. for example.
I use such tricks in quite a few places for working out that sort of complex logic. One example being parsing a query string to work out what tables are in use.
If on Windows, any possibility the the .NET Split method can provide this for you?
Yes you can do that using the Split method of Regular Expression. Example code below:
USING System.Text.RegularExpressions.Regex. DEFINE VARIABLE cSearch AS CHARACTER INIT "cProductNumer = '123' AND cProductName BEGINS 'PEN'" FORMAT "x(60)". DEFINE VARIABLE oRegExp AS CLASS Regex NO-UNDO. DEF VAR liLoop AS INT NO-UNDO. DEF VAR cResult AS CHAR NO-UNDO EXTENT. oRegExp = NEW RegEx("AND"). cResult = oRegExp:Split(cSearch). REPEAT liLoop = 1 TO EXTENT(cResult): DISP TRIM(cResult[liLoop]) FORMAT "x(60)". END.
Developing is on Windows, but the production version is on Linux, I'll have a closer look on Akshay's answer with Regex but thanks!
[mention:77cbb45621a246f9a1a1f2f378e535b1:e9ed411860ed4f2ba0265705b8793d05] also a good idea. Never thought about that^^ But i think the answer form [mention:9ef4e07cd35c4f9686555c3ceee07455:e9ed411860ed4f2ba0265705b8793d05] is the way I'll do it.
Thanks to all!