Friday, April 30, 2010

Escript Framework - lTrim & rTrim

I want to make sure I am making regular contributions to our open source eScript framework function library, so today's theme will be to continue with some tools for the string object not included in the standard escript string library. Last time I talked about lPad and rPad, so let's continue with their inverse, lTrim and rTrim. These are pretty basic concepts, present in VB and SQL, but the idea is simply to remove all spaces from either the front or back of a string. Metalink actually provides a couple of approaches to this, on based on regular expressions from Siebel 7.5 and earlier (before the replace function),
String.prototype.lTrim = function() {
var aReturn = this.split(/^\s*/);
return(aReturn.join(""));
}
String.prototype.rTrim = function() {
var aReturn = this.split(/\s*$/);
return(aReturn.join(""));
}
And here is a looping approach:
String.prototype.rTrim = function() {
var iCount = 0;
var iLength = this.length;
while ((iCount<iLength)&&(this.substring(iLength-iCount-1, iLength-iCount)==" ")) iCount++;
return(this.substring(0, iLength - iCount));
}
String.prototype.lTrim = function() {
var iCount = 0;
var iLength = this.length;
while ((iCount<iLength)&&(this.substring(iCount, iCount+1)==" ")) iCount++;
return(this.substring(iCount, iLength));
}
But if you are using Siebel 7.5 or later, using the replace function with regular expressions that find end of string anchored and beginning of string anchored white space, respectively, should be the best approach:
String.prototype.rTrim = function() {
//Use: String.rTrim()
//Returns string trimmed of any space characters at the end of it
return(this.replace(/\s*$/, ""));
}
String.prototype.lTrim = function() {
//Use: String.lTrim()
//Returns string trimmed of any space characters at the beginning of it
return(this.replace(/^\s*/, ""));
}

3 comments:

  1. Hi Mik,

    I've a real interest in this idea - building a good script framework for Siebel. I realise that script should not be used in place of configuration, but in the cases that you need to, you may as well follow good principles.

    I've been doing a fair bit of this on my own (interest was sparked by early articles saying "a framework exists" (the ATI/ABS framework) - but for probably good reason published code seemed very thin on the ground.

    Rather than wait I decided to have a go at writing my own based on descriptions of what the framework offered, and think I have now a pretty decent and useful framework (it offers both Server and Browser side features).

    Anyway, if you want a hand, or some thoughts on what I've done let me know.

    Cheers, Matt

    (matt DOT h DOT ward AT gmail DOT com)

    ReplyDelete
  2. How can we use this substring function for pre default value

    ReplyDelete
  3. I am not aware of a way to reference them directly, but I use InvokeServiceMethod a lot with a subset of functions geared towards calculated fields, DVM and DTE. Bookshelf has a page that includes syntax:

    http://download.oracle.com/docs/cd/E14004_01/books/ToolsDevRef/ToolsDevRef_Operators8.html

    ReplyDelete