Removing trailing spaces in eScript
Feb/076
At the moment in Siebel, there is no trim function in eScript available to remove leading spacing in a string. In Vbscript, this function is available but not in eScript. A change request has been created to add this function to eScript.
After searching in supportweb, i came acros a piece of code provided by Siebel to replace the Ltrim() function. The code looked like this:
function trim(source)
{
var beginOfString = 0;
var endOfString = 0;for(var index = 0; index < source.length; index++)
{
if(source.charAt(index) != " ")
if(beginOfString == 0)
beginOfString = index;
else
endOfString = index;
}return(source.substring(beginOfString, endOfString + 1));
}
At first, it looked like the code did what it had to do, remove trailing spaces. But then I noticed that when there are no leading spaces, the code also removes the first letter in the string. Since this is unacceptable, I tried to wrote my own code and it turned out to be very simple.
My code looks like this:
function trim(source)
{
while(source.charAt(0) == ” “)
{
source = source.substring(1);
}
return(source);
}
Just a few lines of code but it does the work without removing letters if they are not spaces. Feel free to use the code whenever you need it.
Leave a comment
No trackbacks yet.
7:18 am on March 5th, 2007
Hi mate… greetings from Australia
Nice blog… I’ve been developing in Siebel for 9 months so far. Very intensive training and work, but it sure is good to learn a hell of a lot so quickly.
Anyway… looking at this problem quickly using two functions that come to mind – split and replace – shows that they are more problematic in implementation.
Cheers for the elegant solution. A quick question though… how are the jobs over in Europe?
6:44 pm on March 5th, 2007
Thanks for the compliment.
At this time there are lots of companies looking for Siebel developers. And according to some people who are from outside of Europe, the jos in Europe are very good (and well paid).
8:52 pm on July 7th, 2007
Ok, guys, I know this is a shameless plug, but since you are talking about eScript, I’d like to share a simple tool that I wrote, which is available at our web site…it basically helps you get some idea of what Siebel eScript is really capabale of doing. It’s called the codeFerret and it’s worked out really well for my team.
Here is a link to a blog article that helps you understand what this tool can do for you: http://www.toolsandmethods.com/wordpress
11:15 pm on January 26th, 2009
Here’s a slick one-liner for you, using regular expressions.
function trim (s)
{ // takes a string, removes leading and trailing whitespace
// characters. returns the resulting ‘trimmed’ text.
return (s.replace (/^\s+/, “”).replace (/\s+$/, “”)); // all at once
}
7:12 pm on January 28th, 2009
>> Mike:
Thanks for sharing.
3:32 pm on January 5th, 2010
Hi,
Have a question in escript..
I have a address coming from a website and my requirement is to trim the left and right spaces if there are any.
STREET = ‘ 1493 cordova st ‘
STATE= ‘FL ‘
CITY=’Miami’
COUNTRY= ‘ USA ‘
ZIPCODE= ‘34356-5657 ‘
Could you please let me know how can i do it..