Useful string manipulation functions

Caché comes with a lot of powerful functions to copy, extract and manipulate strings. But anyway, I was always searching the missing function ! Here are some that you could find useful, if you didn't write them already.

For newbies:
 * To add these functions, go in Caché Studio and right-click on Routine in the project window. Add a new routine, paste the code and save your routine with .mac extension.
 * To use them in a class, use the syntax:
 * s result= $$myfunction^myroutine
 * s result= $$myfunction^myroutine(arguments)
 * d myfunction^myroutine
 * d myfunction^myroutine(arguments)

Replace
Replace every occurrence of a substring by another substring. The included $TRANSLATE has the default to replace substring with itself. Replace(str,exp1,exp2)	; q:exp1="" str ;one can't replace nothing ! s lg=$L(exp1),lg2=$L(exp2),lastOccur=0 f { //iterate while the substring is found s i=$F(str,exp1,lastOccur) q:i=0 s lastOccur=i-lg+lg2 s str=$E(str,1,i-lg-1)_exp2_$E(str,i,$L(str)) }	q str
 * Arguments:
 * str: the source string where the replacement is done
 * exp1: the substring to replace in str
 * exp2: the new substring to put in place of exp1
 * Returns:the replaced str string

FormatString
Replaces every occurence of % by the successive strings argn like in C. FormatString(str,arg1="",arg2="",arg3="",arg4="",arg5="",arg6="",arg7="",arg8="") s retstr="",arg9="" f i=1:1 { s sub=$P(str,"%",i) q:sub="" s var="arg"_i s retstr=retstr_sub_@var }	q retstr
 * Arguments:
 * str: the source string to be filled
 * arg1..8: strings to replace the % in str
 * Returns:filled str

Find
Search a substring like $FIND, but matches only the real substring, like a word parser.

For example:
 * $FIND( "abcdef", "cde" ) returns 6
 * $$Find^tools( "abcdef", "cde" ) returns 0 because cde is not surrounded by allowed separators
 * $$Find^tools( "ab,cde f", "cde" ) returns 7

Find(str,strToFind,separators) s separators=$G(separators," /-.,;*=~") s pos=1,foundPos=0 f { s pos=$F(str,strToFind,pos) q:pos<1 s posDebut=pos-$L(strToFind) i (posDebut<1)||($F(separators,$E(str,posDebut-1))>0),(pos>$L(str))||($F(separators,$E(str,pos))>0) s foundPos=pos q:foundPos>0 }	q foundPos
 * Arguments:
 * str: source string
 * strToFind: substring to search in str
 * separators: string containing the characters allowed as separators between two words. By default, standard separators are provided.
 * Returns:the charecter position after the found substring (like $FIND), or 0 if not found

CharString
Fill a string with n identical characters CharString(char,n) new a	s $P(a,char,n+1)="" q a
 * Arguments:
 * char : character to fill with
 * n : number of chars
 * Returns:string filled with char

function
description
 * Arguments:
 * arg:
 * Returns: