Split a string by string

Posted by Dennis on 29-Nov-2017 02:05

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. 

Posted by Akshay Guleria on 29-Nov-2017 04:46

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.

All Replies

Posted by Mike Fechner on 29-Nov-2017 02:10

The ENTRY function?

Sent from Nine

Von: "bezold1@imas-net.de" <bounce-bezold1imas-netde@community.progress.com>
Gesendet: 29.11.2017 09:06
An: TU.OE.Development@community.progress.com
Betreff: [Technical Users - OE Development] Split a string by string

Update from Progress Community
bezold1@imas-net.de

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. 

View online

 

You received this notification because you subscribed to the forum.  To stop receiving updates from only this thread, go here.

Flag this post as spam/abuse.

Posted by Dennis on 29-Nov-2017 03:43

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".

Posted by James Palmer on 29-Nov-2017 04:01

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.

Posted by oedev on 29-Nov-2017 04:34

If on Windows, any possibility the the .NET Split method can provide this for you?

msdn.microsoft.com/.../system.string.split(v=vs.110).aspx

Posted by Akshay Guleria on 29-Nov-2017 04:46

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.

Posted by Dennis on 29-Nov-2017 05:12

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!

Posted by Dennis on 29-Nov-2017 05:16

[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!

This thread is closed