Question Krishnaveni Kapu · Apr 23, 2024

subString before last delimeter

How can I  extract the sub-string before the last occurance of a delimeter in a string.

string = "aaDtext1Dtext2"

Delimeter = D

answer I need = aaDtext1

Comments

Roger Merchberger · Apr 23, 2024

The $LENGTH function can give you the number of PIECEs in a string, and the $PIECE function can grab multiple pieces, so for your example:

S DELIM="D"S STRING="aaDtext1Dtext2"W$PIECE(STRING,DELIM,1,($LENGTH(STRING,DELIM)-1))

Edit: Links to documentation for the $PIECE and $LENGTH functions:

$PIECE: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_fpiece

$LENGTH: https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_flength

Hope this helps!

0
Enrico Parisi  Apr 23, 2024 to Roger Merchberger

Within $piece, instead of $LENGTH(STRING,DELIM) you can use *

0
Roger Merchberger  Apr 23, 2024 to Enrico Parisi

With more recent versions, yes. The user didn't state what version Ensemble was in use, so I figured offering a solution that would work as far back as Ensemble 2009 wouldn't necessarily be a bad thing. I still have to support Ensemble 2012 and I did test the *-1 functionality there, and in that version this functionality did not yet exist. (Actually, a few sites I support are still using Cache 5.x. It may be geriatric, but it keeps on plugging along... :-) )

0