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?

00
5 0 10 717
Log in or sign up to continue

set name = "ERRORMSG"

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

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

Thank you Pavel,

I like this one ^^

Cheers,

Jacques

This answer  by Pavel is the simplest way. :)

Why "-0"? 

Just "*" will be enough:

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

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?KEY...

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?KEY...

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

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

Also this option is available :

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

Perhaps tricky for someone :-)