<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yesJames.com &#187; web design</title>
	<atom:link href="http://www.yesjames.com/index.php/tag/web-design/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.yesjames.com</link>
	<description>ahuh... sure... what ever you say...</description>
	<lastBuildDate>Fri, 21 Oct 2011 05:16:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>JavaScript Trim() function</title>
		<link>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/</link>
		<comments>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/#comments</comments>
		<pubDate>Thu, 20 Mar 2008 05:25:14 +0000</pubDate>
		<dc:creator>james</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[trim]]></category>
		<category><![CDATA[web design]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.yesjames.com/index.php/2008/03/20/javascript-trim-function/</guid>
		<description><![CDATA[Trim(), is one function that we all use almost every day in most languages, whether it&#8217;s a core function of the language you&#8217;re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it&#8217;s invaluable for developers. Unfortunately JavaScript doesn&#8217;t have this sort of thing built in, so [...]]]></description>
			<content:encoded><![CDATA[<p>Trim(), is one function that we all use almost every day in most languages, whether it&#8217;s a core function of the language you&#8217;re using or a member (or prototype in JavaScript) function of the String object itself (as in C#), it&#8217;s invaluable for developers.</p>
<p>Unfortunately JavaScript doesn&#8217;t have this sort of thing built in, so here&#8217;s how to implement your own <em>trim()</em> functions (as well as ltrim() and rtrim()) in various ways depending on the level of backward compatibility you require.</p>
<p><span id="more-42"></span></p>
<h3>trim() member functions</h3>
<p>Use the code below to make trim a method of all Strings. Ordinarily you would place this code into a global JavaScript file that would be included in all your pages.</p>
<pre name="code" class="brush: javascript">
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
</pre>
<h4>Examples using trim(), ltrim(), and rtrim() prototyped methods</h4>
<pre name="code" class="javascript">
var testString = " hello world ";
alert("#"+ testString.trim() +"#");
alert("#"+ testString.ltrim() +"#");
alert("#"+ testString.rtrim() +"#");
</pre>
<h3>standalone JavaScript functions</h3>
<p>If you&#8217;d prefer to use regular functions to return trimmed strings rather than modifying the String object using prototyping, then try the following code:</p>
<pre name="code" class="brush: javascript">
function trim(value) {
    return value.replace(/^\s+|\s+$/g,"");
}
function ltrim(value) {
    return value.replace(/^\s+/,"");
}
function rtrim(value) {
    return value.replace(/\s+$/,"");
}
</pre>
<h4>Examples using trim(), ltrim(), and rtrim() functions</h4>
<pre name="code" class="brush: javascript">
var testString = " hello world ";
alert("#"+ trim(testString) +"#");
alert("#"+ ltrim(testString) +"#");
alert("#"+ rtrim(testString) +"#");
</pre>
<h3>Compatibility</h3>
<p>The above functions require JavaScript 1.2+ and JScript 3.0+ as they are using Regular Expressions to work their magic. This pretty much covers all modern browsers (v4 and above of IE and opera, and all versions of Firefox).<br />
If you require functions that will be compatible with very old browsers only supporting JavaScript 1.0 or there about, try using the following set of functions. These functions strip the following standard whitespace characters: space, tab, line feed, carriage return, and form feed. The IsWhitespace function checks if a character is whitespace.</p>
<pre name="code" class="brush: javascript">
function ltrim(str) {
    for(var k = 0;
        k &lt; str.length &amp;&amp; isWhitespace(str.charAt(k));
        k++);
    return str.substring(k, str.length);
}
function rtrim(str) {
    for(var j=str.length-1;
        j&gt;=0 &amp;&amp; isWhitespace(str.charAt(j));
        j--);
    return str.substring(0,j+1);
}
function trim(str) {
    return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.yesjames.com/index.php/2008/03/javascript-trim-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

