Written by

Question ED Coder · Oct 17, 2018

Removing the first two characters in a string

Hi,

This must be a really easy question but for some reason I am unable to do this

I have a string  name = "ERRORMSG"

I want to remove the first 2 characters in the string, so the result should be string newname = RORMSG

Please can you advice on how I can do this?

Comments

CARLOS COSTA  Oct 17, 2018 to Sean Connelly

set name = "ERRORMSG"

set name = $E(name,3,$L(name))

0
ED Coder  Oct 17, 2018 to Sean Connelly

Hi Sean, thank you for replying to my request. Using the above code, I am getting just RORMS, I am not getting the last character.

$e(name,3,*-0) worked.

Thank you so much. I am grateful

0
Jack Huser  Oct 17, 2018 to Pavel Bezstarosti

Thank you Pavel,

I like this one ^^

Cheers,

Jacques

0
Larry Faraci  Nov 1, 2018 to Pavel Bezstarosti

This answer  by Pavel is the simplest way. :)

0
Evgeny Shvarov  Oct 17, 2018 to CARLOS COSTA

Why "-0"? 

Just "*" will be enough:

set name=$E(name,3,*)
0
Jack Huser · Oct 17, 2018

Hello,

There is several method to do so... depending of the context of your needs... if your error msg is at the beginning of the line, in the input text or alone...

You can use extract:

set mystring = "ERRORMSG"

write $extract(mystring,3,*), !

see: https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KE…

this method would not be used if your input string does not start with "ERRORMSG"

if you know exactly that you want to remove ER from ERRORMSG you can use $replace

set mystring = "this is an ERRORMSG"

write $replace(mystring, "ERRORMSG", "RORMSG"), !

see: https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KE…

you can use a $piece : https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KE…

set mystring = "this is an ERRORMSG"

set textToRemove = "ER"

write $piece(mystring,textToRemove)_$piece(mystring,textToRemove,2), !

Though this last method would have a problem is the string "ER" is repeated in the input string

I hope this help

Cheers,

Jacques

0
Pavel Bezstarosti · Oct 17, 2018

Also this option is available :

set $extract(name,1,2)=""

Perhaps tricky for someone :-)

0