devxlogo

Trim the Leading and Trailing Spaces in a Given String

This functionality is not currently available in Javascript. However, tou can add this function to perform trimming in the strings. Trimming is useful in checking for empty spaces.

 function RTrim(str){	var whitespace = new String(" 	

");	var s = new String(str);	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {	    var n = s.length - 1;	    while (n >= 0 && whitespace.indexOf(s.charAt(n)) != -1){	        n--;			s = s.substring(0, n+1);		}	}	return s;}function LTrim(str){	var whitespace = new String(" 	

");	var s = new String(str);	if (whitespace.indexOf(s.charAt(0)) != -1) {	    var pos=0, m = s.length;	    while (pos < m && whitespace.indexOf(s.charAt(pos)) != -1){			pos++;			s = s.substring(pos, m);		}	}	return s;}function Trim(s){	if (s=="undefined")		return "";	else{		var tmp;		tmp = RTrim(LTrim(s));		return tmp;	}}

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.