<?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>Barryvan &#187; string manipulation</title>
	<atom:link href="http://www.barryvan.com.au/tag/string-manipulation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.barryvan.com.au</link>
	<description>Music, Programming, Design</description>
	<lastBuildDate>Sun, 08 Jan 2012 08:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Javascript string ellipsising</title>
		<link>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/</link>
		<comments>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 07:03:17 +0000</pubDate>
		<dc:creator>Barry van Oudtshoorn</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web design]]></category>
		<category><![CDATA[string manipulation]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.barryvan.com.au/?p=217</guid>
		<description><![CDATA[Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you. This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters: toLength (required) [...]]]></description>
			<content:encoded><![CDATA[<p>Putting ellipses into strings that are too long has been around for a very long time. Unfortunately, Javascript doesn&#8217;t offer a native method of doing this, so below is a little function that&#8217;ll do it for you.</p>
<p>This function returns a copy of the string it&#8217;s called on, ellipsised, and takes three parameters:</p>
<p><strong>toLength</strong> (required) The number of characters to truncate the string to (or 0 to disable ellipsising)</p>
<p><strong>where</strong> (optional, default &#8216;end&#8217;) A string representing where the ellipsis should be placed &#8212; &#8216;front&#8217;, &#8216;middle&#8217;, or &#8216;end&#8217;</p>
<p><strong>ellipsis</strong> (option, default &#8216;\u2026&#8242;) A string to be used as the ellipsis.</p>
<h2>Examples</h2>
<pre class="brush: javascript">
// Our clichéd string
var s = &#039;Jackdaws love my great big sphinx of quartz&#039;;

alert(s.ellipsise(10));
// Alerts &quot;Jackdaws l…&quot;

alert(s.ellipsise(10, &#039;front&#039;));
// Alerts &quot;… of quartz&quot;

alert(s.ellipsise(10, &#039;middle&#039;, &#039;pony&#039;));
// Alerts &quot;Jackdponyuartz&quot;</pre>
<h2>The code</h2>
<pre class="brush: javascript">String.implement({
	ellipsise: function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
		if (toLength &lt; 1) return this;
		ellipsis = ellipsis || &#039;\u2026&#039;;
		if (this.length &lt; toLength) return this;
		switch (where) {
			case &#039;front&#039;:
				return ellipsis + this.substr(this.length - toLength);
				break;
			case &#039;middle&#039;:
				return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
				break;
			case &#039;end&#039;:
			default:
				return this.substr(0, toLength) + ellipsis;
				break;
		}
	}
});</pre>
<p>If you&#8217;re not using MooTools, you can use this variant instead:</p>
<pre class="brush: javascript">String.prototype.ellipsise = function(toLength, where, ellipsis) { // Where is one of [&#039;front&#039;,&#039;middle&#039;,&#039;end&#039;] -- default is &#039;end&#039;
	if (toLength &lt; 1) return this;
	ellipsis = ellipsis || &#039;\u2026&#039;;
	if (this.length &lt; toLength) return this;
	switch (where) {
		case &#039;front&#039;:
			return ellipsis + this.substr(this.length - toLength);
			break;
		case &#039;middle&#039;:
			return this.substr(0, toLength / 2) + ellipsis + this.substr(this.length - toLength / 2)
			break;
		case &#039;end&#039;:
		default:
			return this.substr(0, toLength) + ellipsis;
			break;
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.barryvan.com.au/2009/08/javascript-string-ellipsising/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

