devxlogo

Trim the Leading and Trailing Spaces in a Given String

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;	}}
See also  Why ChatGPT Is So Important Today
devxblackblue

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.

About Our Journalist