Question Sreevani Goli · Jan 11, 2024

How to strip the last char from a string in the DTL

I would like to strip the , at the end of a string. I'm able to find the start of the string using ..StartsWith(String,",") but not able to strip the "," at the end of a string.

I've tried:

Str="Addr1,Addr2,"

Piece(Str,"",1,Length(Str)-1)

Piece(Str,"",1,Length(Str)-2)

Extract(Str, Length(Str)-1)

Extract(Str, Length(Str))

But nothing works as expected.

Product version: IRIS 2022.1
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 7 for x86-64) 2022.1 (Build 209U) Tue May 31 2022 12:13:58 EDT [HealthConnect:3.5.0] [HealthConnect:3.5.0]

Comments

David Hockenbroch · Jan 11, 2024

When using $EXTRACT, you use a * to signify an offset from the end of a string. So if you did $EXTRACT(Str,1,*-1) you would have the string with the last character removed.

Also note that the arguments for the $EXTRACT are the string, the starting character, and the ending character, so in the examples you gave, you're actually telling it to extract from Str starting at Length(Str)-1. You need to have a 1 in there as the second argument to go from the beginning to that character.

0
Ben Spead · Jan 11, 2024

@Sreevani Goli - if you know it is comma delimited and you want to strip off the last comma, you could use $piece() as follows:

USER>s Str="Addr1,Addr2,"
 
USER>write $piece(Str,",",1,*-1)
Addr1,Addr2

This will basically grab the entire string except for the last comma (and anything that comes after it).  

0
Sreevani Goli · Jan 11, 2024

Thank you David and Ben.

I've tried using the below in DTL but it throws the compilation error.

$piece(Str,",",1,*-1)

(Transform+103) #1054: Invalid expression : 'patAdr22=..Piece(patAdr2,,1,*-1)' :

Is it that it should work fine when I write an ObjectScript function and call it from DTL ?

Also, I tried with: 

patAdr22=..Piece(patAdr2,,1,..Length(patAdr2))

patAdr22=..Piece(patAdr2,,1,..Length(patAdr2)-1)

But no luck.

0
Ben Spead  Jan 11, 2024 to Sreevani Goli

@Sreevani Goli - I am not sure about the DTL syntax, but looking at the error it looks like the "," is being dropped from the 2nd argument.  Maybe you need to try using "","" to escape the double quotes around the comma?

0
Sreevani Goli · Jan 11, 2024

Thanks David and Ben for your help. I wrote an ObjectScript method based on your suggestions:

($E(value,1,$L($g(value))-$L($g(string))))

And it's working fine. Thanks again for your help.

0
Ben Spead  Jan 11, 2024 to Sreevani Goli

glad to hear you got it working!

0
Julius Kavay · Jan 11, 2024

In general, $extract() and $zstrip() are your friends.
If you want to strip ONLY the LAST character, then use this

set data="abc,,"set$extract(data,*)=""write data --> abc,

If you want to strip ALL (same) trailing characters, use this

set remove=","set data1="abc,"set data2="abc,,,"set data3="abc,,-,,"set data1=$zstrip(data1,">",remove)
set data2=$zstrip(data2,">",remove)
set data3=$zstrip(data3,">",remove)

write data1 --> abc
write data2 --> abc
write data3 --> abc,,-
0