Trimming...
Hello, guys.
I found one interesting moment in Cache Object Script. It doesn't have(or at least I didn't find) trimming function. By trimming I mean if a string has some whitespaces/tabs/carriage returns from very beginning or/and from very right, this function removes them.
I have found several workaround ways.
1. Using Cache Basic
ClassMethod TrimCacheBasic(str As %String) As %String [ Language = basic ] { Return Trim(str) }
2. Using SQL
ClassMethod TrimSQL(str As %String) As %String { set tStatement = ##class(%SQL.Statement).%New() set tSC = tStatement.%Prepare("SELECT TRIM(?)") set rset = tStatement.%Execute(str) do rset.%Next() return rset.%GetData(1) }
3. Using $extract
Just iterate over every character like $e(str, 1, 1), $e(str, 2, 2)... until we meet any non-whitespace/tab/carriage return character. And the same action from the end.
Is there any better way to do this using ordinary COS or workaround are the only option? IMHO, I consider the first option to be the best.
$zstrip (documentation) can do trimming.
Yes, $ZStrip. For example:
Write $ZStrip(" ,a b, ","<>W") >,a b, Write $ZStrip(" ,a b, ","<>WP") >a b
Thanks, didn't think about it
If you want to get rid from spaces in the string you can use $translate too.
USER> set s=" The string with spaces " USER> write $translate(str," ", ) Thestringwithspaces
$TR removes all the spaces from the complete string, which is not what was asked for. $ZSTRIP is the way to go.
You are right, Fab. I just like $Translate too much ;)