Question
· 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?

Discussion (10)2
Log in or sign up to continue

yes typing too fast, should have pointed out the -1 was for reference to trim at the other end

you can get a full list of these type of functions here...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_FUNCTIONS

and a bit more information specifically on working with strings here...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_strings

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