Question
· Dec 18, 2016

How to exclude substring(s) from a string

Hi, community!

Consider you need to exclude substring(s) from a string.

I did it with the following snippet:

/// excludes all the substrings from the string
ClassMethod ExcludeSubstring(substr,str as %String) As %String

{

 while ($L(str,substr)>1) {

 set str=$Piece(str,substr)_$Piece(str,substr,2,*)

            }

 quit str

}

So, for example:

USER> set str=”ExcludemeGoodstringExcludemeExcludemeGoodstring” w ##class(User.Utils).ExcludeSubstring(“Excludeme”,str)

GoodstringGoodstring

Is this the optimal approach or I reinvented the wheel?

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