Trim functionality on web in JavaScript

Trim functionality on web in JavaScript

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);
}

USER FEEDBACK TO THIS TIP

  • 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.

    By submitting your registration information to SearchDomino.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchDomino.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

  • = 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

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.