Friday, April 6. 2012
Truncating text while respecting word boundaries.
Every so once in a while the issue rises where you need to truncate a piece of text to a certain maximum length.
Now that in itself is not so hard to do. But the result can look quite strange when the truncation point happens in the middle of a word.
As always StackOverflow provides a good starting point.
I just thought using a Regular Expression would be more concise. So here is my solution:
private const string Ellipsis = "...";
// Match word boundaries at the end of a word.
private static Regex wordRegex = new Regex(@"(?<=\w)(?!\w)", RegexOptions.Compiled);
private static string Truncate(string source, int length)
{
int maxLength = length - Ellipsis.Length;
if (source != null && source.Length > length)
{
int afterLastWordIndex = 0;
foreach (Match m in wordRegex.Matches(source))
{
if (m.Index <= maxLength)
{
afterLastWordIndex = m.Index;
}
else
{
break;
}
}
return String.Concat(source.Substring(0, afterLastWordIndex), Ellipsis);
}
return source;
}
static void Main(string[] args)
{
string source = "The quick brown fox jumps over the lazy dog";
Console.WriteLine(" 1 2 3 4 5");
Console.WriteLine("12345678901234567890123456789012345678901234567890");
Console.WriteLine(source);
int[] truncingLengths = { 0, 1, 3, 4, 8, 9, 10, 15, 19, 20, 21, 22, 23, 28, 30, 33, 42, 43, 50 };
foreach (int truncAt in truncingLengths)
{
Console.WriteLine("{0}: \"{1}\"", truncAt, Truncate(source, truncAt));
}
Console.ReadLine();
}
Now that in itself is not so hard to do. But the result can look quite strange when the truncation point happens in the middle of a word.
As always StackOverflow provides a good starting point.
I just thought using a Regular Expression would be more concise. So here is my solution:
private const string Ellipsis = "...";
// Match word boundaries at the end of a word.
private static Regex wordRegex = new Regex(@"(?<=\w)(?!\w)", RegexOptions.Compiled);
private static string Truncate(string source, int length)
{
int maxLength = length - Ellipsis.Length;
if (source != null && source.Length > length)
{
int afterLastWordIndex = 0;
foreach (Match m in wordRegex.Matches(source))
{
if (m.Index <= maxLength)
{
afterLastWordIndex = m.Index;
}
else
{
break;
}
}
return String.Concat(source.Substring(0, afterLastWordIndex), Ellipsis);
}
return source;
}
static void Main(string[] args)
{
string source = "The quick brown fox jumps over the lazy dog";
Console.WriteLine(" 1 2 3 4 5");
Console.WriteLine("12345678901234567890123456789012345678901234567890");
Console.WriteLine(source);
int[] truncingLengths = { 0, 1, 3, 4, 8, 9, 10, 15, 19, 20, 21, 22, 23, 28, 30, 33, 42, 43, 50 };
foreach (int truncAt in truncingLengths)
{
Console.WriteLine("{0}: \"{1}\"", truncAt, Truncate(source, truncAt));
}
Console.ReadLine();
}
« previous page
(Page 1 of 1, totaling 1 entries)
next page »