Question
· Jan 11

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]
Discussion (8)2
Log in or sign up to continue

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.

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.

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,,-