<?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>Michael E. Chancey Jr. &#187; Truncate</title>
	<atom:link href="http://michael.chanceyjr.com/tag/truncate/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.chanceyjr.com</link>
	<description></description>
	<lastBuildDate>Fri, 20 May 2011 03:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>String Truncating Extension</title>
		<link>http://michael.chanceyjr.com/useful-code/string-truncating-extension/</link>
		<comments>http://michael.chanceyjr.com/useful-code/string-truncating-extension/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 10:12:00 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Truncate]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=539</guid>
		<description><![CDATA[This is a simple extension to truncate strings on whole words. If it can not find a whole word to truncate on then it will fall back to breaking a word up. You can also pass a string to append to the end of the truncated string. Other then that there is not a whole [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple extension to truncate strings on whole words.  If it can not find a whole word to truncate on then it will fall back to breaking a word up.  You can also pass a string to append to the end of the truncated string.  Other then that there is not a whole lot to say about this one.</p>
<h3>//Code</h3>
<hr/>
<pre class="brush: csharp;">
        /// &lt;summary&gt;
        /// TRUNCATES A STRING PREFERRING WHOLE WORD OVER NON WHOLE WORD
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;s&quot;&gt;INCOMING STRING TO TRUNCATE&lt;/param&gt;
        /// &lt;param name=&quot;len&quot;&gt;LENGTH IN WHICH TO TRUNCATE TO&lt;/param&gt;
        /// &lt;param name=&quot;postTruncate&quot;&gt;ANYTHING TO ADD TO A TRUNCATED STRING SUCH AS ...&lt;/param&gt;
        /// &lt;returns&gt;TRUNCATED STRING WITH POST TRUNCATE ADDED TO IT&lt;/returns&gt;
        public static string Truncate(this string s, int len, string postTruncate)
        {
            //IF THE STRING IS SMALLER THEN THE LENGTH JUST RETURN THE STRING
            if (s == null || s.Length &lt; len)
                return s;
            else
            {
                //INCASE NULL HAS BEEN PASSED IN CHANGE IT TO EMPTY STRING SO IT HAS A COUNT
                if (postTruncate == null)
                    postTruncate = string.Empty;

                //RETURN THE TRUNCATED STRING BACK TO CALLING FUNCTION
                string tmpReturn = string.Concat(Regex.Match(String.Concat(s, &quot; &quot;), string.Format(&quot;^(?&lt;tmpMatch&gt;.{{0,{0}}})\\s&quot;, len)).Groups[&quot;tmpMatch&quot;].Value, postTruncate);

                //IF THE WHOLE WORD TRUNCATE WORKED THEN USE IT OTHERWISE USE NON WHOLE WORD TRUNCATE
                if (tmpReturn != postTruncate)
                    return tmpReturn;
                else
                    return string.Concat(Regex.Match(String.Concat(s, &quot; &quot;), string.Format(&quot;^(?&lt;tmpMatch&gt;.{{0,{0}}})&quot;, len)).Groups[&quot;tmpMatch&quot;].Value, postTruncate);
            }
        }
</pre>
<h3>//Usage</h3>
<hr/>
<pre class="brush: csharp;">
    class Program
    {
        static void Main(string[] args)
        {
            //OUTPUT WILL BE &quot;Hello, World...&quot;
            Console.WriteLine(&quot;Hello, World this is some long text to truncate&quot;.Truncate(15, &quot;...&quot;));
            Console.ReadLine();
        }
    }
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=String%20Truncating%20Extension%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension&amp;bodytext=This%20is%20a%20simple%20extension%20to%20truncate%20strings%20on%20whole%20words.%20%20If%20it%20can%20not%20find%20a%20whole%20word%20to%20truncate%20on%20then%20it%20will%20fall%20back%20to%20breaking%20a%20word%20up.%20%20You%20can%20also%20pass%20a%20string%20to%20append%20to%20the%20end%20of%20the%20truncated%20string.%20%20Other%20then%20that%20th" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fstring-truncating-extension%2F&amp;title=String%20Truncating%20Extension&amp;notes=This%20is%20a%20simple%20extension%20to%20truncate%20strings%20on%20whole%20words.%20%20If%20it%20can%20not%20find%20a%20whole%20word%20to%20truncate%20on%20then%20it%20will%20fall%20back%20to%20breaking%20a%20word%20up.%20%20You%20can%20also%20pass%20a%20string%20to%20append%20to%20the%20end%20of%20the%20truncated%20string.%20%20Other%20then%20that%20th" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/string-truncating-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

