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
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
USER>set string = "aaDtext1Dtext2"
USER>write $p(string,"D",1,*-1)
aaDtext1
Edited to add relevant documentation link.
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!
Within $piece, instead of $LENGTH(STRING,DELIM) you can use *
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... :-) )