Posts tagged C#
String extension methods for truncating and adding ellipsis
Feb 2nd
Some useful extension methods for working with strings and IEnumerable<string>. These come in handy when logging as you often want to show just 80 characters (say) in a log entry.
/// <summary>
/// Substring but OK if shorter
/// </summary>
public static string Limit(this string str, int characterCount)
{
if (str.Length <= characterCount) return str;
else return str.Substring(0, characterCount).TrimEnd(‘ ‘);
}
/// <summary>
/// Substring with elipses but OK if shorter, will take 3 characters off character count if necessary
/// </summary>
public static string LimitWithElipses(this string str, int characterCount)
{
if (characterCount < 5) return str.Limit(characterCount); // Can’t do much with such a short limit
if (str.Length <= characterCount – 3) return str;
else return str.Substring(0, characterCount – 3) + “…”;
}
/// <summary>
/// Substring with elipses but OK if shorter, will take 3 characters off character count if necessary
/// tries to land on a space.
/// </summary>
public static string LimitWithElipsesOnWordBoundary(this string str, int characterCount)
{
if (characterCount < 5) return str.Limit(characterCount); // Can’t do much with such a short limit
if (str.Length <= characterCount – 3)
return str;
else
{
int lastspace = str.Substring(0, characterCount – 3).LastIndexOf(‘ ‘);
if (lastspace > 0 && lastspace > characterCount – 10)
{
return str.Substring(0, lastspace) + “…”;
}
else
{
// No suitable space was found
return str.Substring(0, characterCount – 3) + “…”;
}
}
}
The Blog of Ian Mercer: Home Automation++
Jan 26th
This is the blog of Ian Mercer, Serial entrepreneur, Digital Mapping Pioneer, Inventor of Windows Movie Maker, Microsoft Producer, HighMat, SnapTune, … Patent holder, Guinness world record holder, and founder and CEO of SignSwift, LLC,
Here you’ll find my thoughts on Home automation, Energy Conservation, Programming, Operations, Entrepreneurship, Business, Startups, Quality, Agile, Optimization, .NET, Natural Language Processing, Digital Signage and more.
My home automation system recently won 1st and 3rd place in the international, Microsoft MyDotNetStory competition. You can view a demonstration of it under the home automation menu above. I believe that it is the world’s smartest house because I haven’t seen anything smarter. It controls lights, heating, sprinklers, audio, and more; it has hundreds of sensors including PIR, strain gauges, door switches, and more; it interfaces to Caller ID, PBX, Google Calendar, Google Contacts, Email, NOAA, Twitter, Facebook, RSS, Traffic, Podcasts and more. For the most part it works autonomously doing the right thing and reducing our energy consumption by over 40%. You can control it using a web, voice or chat interface. It has a unique Natural Language Interface that understands sentences and can hold a two-way conversation with you.
Under the SEO menu you’ll find my SEO keyword mapping tool; it can untangle a list of keyword phrases and volumes (like you get from Google’s Adwords tools) to give you a clearer picture of the keywords you need to put on your pages. Using variable sized boxes and lines it shows you which words are the most important and how they are linked.
Thoughts under programming are mostly related to Microsoft’s .NET platform and the C# language but there are some that are more general in nature. I’ve been using Visual Studio 2010 and .NET 4 extensively recently including the Entity Framework, WCF, WPF and Silverlight.
You can view my professional profile on Linkedin or follow me on Twitter.



I’m also an avid photographer and videographer and I’m in the process of adding a few of my favorite pictures to the site.
Tip: getting the index in a foreeach statement
Dec 10th
Using LINQ you can easily get at the index in a foreach statement.
List x = new List() {"a", "b", "c"};
var augmented = x.Select((item, index) => new { item = s, index = index });
foreach (var d in augmented)
{
Console.WriteLine(d.item + " " + d.index);
}
Console.ReadKey();
a 0
b 1
c 2
Interesting or useful links on Twitter this week relating to .NET and C#
Feb 22nd
Posted by Ian Mercer in Commentary