Question
· Sep 9, 2016

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.

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