Written by

Question Yone Moreno · Jun 25, 2024

How to remove line feed and carriage return from start and end, but not from the middle of a string

Hello,

Thanks for reading this question.

We need to remove the line feeds and carriage returns from start and end, but not from the middle of a string

We have tested the following ways:

set output = $ZStrip("[line feed](carriage return) str [line feed](carriage return) ing [line feed](carriage return)","<>C")

But it removes also control characters which we need to preserve...

Also we have tested:

set output = $REPLACE("[line feed](carriage return) str [line feed](carriage return) ing [line feed](carriage return)",$C(13,10),"")

But it deletes line feed and carriage return at the middle of the string, so it is not what we need...

How would you recommend us to implement it.

We have read:

https://community.intersystems.com/post/trimming

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…

https://community.intersystems.com/post/zstrip-clean-string

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cl…

Could you help us please?

Thanks.

Product version: IRIS 2020.1

Comments

Stuart Strickland · Jun 25, 2024

F  Q:$c(10,13)'[$e(str)!'$L(str)  s $e(str)=""  // strip $c(10) and $c(13) from front

F  Q:$c(10,13)'[$e(str,*)!'$l(str)  s $e(str,*)=""  // strip $c(10) and $c(13) from end

0
Yone Moreno  Jun 25, 2024 to Stuart Strickland

There are multiple line feeds and carriage returns not only one at start and at end

0
Stuart Strickland  Jun 25, 2024 to Yone Moreno

Yes, and it will remove all those from the front and all those from the end, preserving those in the middle.

0
Ben Spead · Jun 25, 2024

If you know that it begins and ends with CRLF then it is as simple as:

USER>Set string = "[line feed](carriage return) str [line feed](carriage return) ing [line feed](carriage return)"
 
USER>Write $piece(string,"[line feed](carriage return)",2,*-1)
 str [line feed](carriage return) ing
0
Yone Moreno  Jun 25, 2024 to Ben Spead

There are multiple line feeds and carriage returns not only one at start and at end

0
Ben Spead  Jun 25, 2024 to Yone Moreno

Yes - this will work in that case above case (see my example which preserves all of them in the middle)

0
Brett Saviano · Jun 25, 2024

@Yone Moreno 
Did you try $ZSTRIP with the following arguments?

Set string = $ZSTRIP(string,"<>",$CHAR(13,10))

$ZSTRIP without a mask code in the second argument and with specific characters as the third argument will strip just those specific characters.

0