You Can View User Feedback To This Tip
Many times we wish that how nice it would be if we would have @Trim functionality in JavaScript.
You can have the same. Use this trim(strln) function. Just put this function in the JSHeader and call the same like this.
trim(document.forms[0].fieldName.value);
This will trim the whole value, like @Trim function.
//Trim Function
function trim(strIn)
{
strOut = strIn;
for (var t = 0; t <= (strIn.length -1); t++ )
{
if ( strIn.charAt(t) != " ")
{
strIn = strOut;
for (var I = strIn.length-1 ;I >= 0 ; I-- )
{
if ( strIn.charAt( I ) != " ")
{
return (strOut) ;
}
else
{
strOut = strIn.substring(0, I );
}
}
}
else
{
strOut = strIn.substring( t+1, strIn.length);
}
}
return(strOut);
}
- I have seen a TIP for @TRIM posted on 07/18/2001 and I tried to use it but it is not removing the redundant spaces in between the characters like " aaa bbb "to "aaa bbb." You can use this JavaScript code in place of the @TRIM formula language function. It will remove all leading, trailing & redundant spaces between a string.
function trim(inputString) { //String1
Requires Free Membership to View
Register today to access targeted resources from our editorial writers and independent industry experts focused on Lotus Domino, Notes, Workplace and other related technologies.
= inputString.replace(/^s+/,""); // trim leading white space // this JAVA code is not working sometime with domino. //String2 = String1.replace(/s+$/,""); // trim trailing white space //this JAVA code is not working sometime with domino. // added another condition in the end to remove ending space. finalText = ""; lastChar = " "; for(x = 0; x < inputString.length; x++) // with this loop we will delete all redundant spaces { curChar = inputString.charAt(x); if ((curChar != " ") || (lastChar != " ")) { finalText += curChar; } lastChar = curChar; } if (finalText.charAt(finalText.length - 1) == " ") // this will remove the ending/trailing space. { finalText = finalText.substring(0, finalText.length - 1); } return finalText; } Faran Siddiqy
This was first published in July 2001