I've received a request at work and the senior developer is away on vacation so I can't ask him. The request was to truncate lengthy article titles while maintaining readability. In other words, don't simply chop a word in half and add an ellipsis.
I wrote an extension method that looks for punctuation and establishes length requirements.
Bullet Points:
- truncate at sentence endings ('.','!','?')
- allow for variable maxLength
- make sure title is at least 30 chars long (for the case where a title starts like "wow!")
- when all else fails truncate at last space and add an ellipsis
Anyway, the method works the way I intended, however, the programming feels gross.
What is a more succinct way of writing this string extension method?
I've reproduced the code in a fiddle: .NET Fiddle
Extension method
public static class StringExtensions
{
public static string TruncateAtFirstSentence(this string value, int maxLength)
{
char[] punc = new char[3];
punc[0] = '.';
punc[1] = '!';
punc[2] = '?';
// not the best programming, basically falls though when requirements
// arent met.
foreach (char c in value) {
if (c == punc[2]) {
var str = value.Substring(0, value.IndexOf("?") + 1);
if (str.Length <= 30)
continue;
else
{
if (str.Length > maxLength)
{
continue;
}
else
{
return str;
}
}
}
else if (c == punc[1]) {
var str = value.Substring(0, value.IndexOf("!") + 1);
if (str.Length <= 30)
continue;
else
{
if (str.Length > maxLength)
{
continue;
}
else
{
return str;
}
}
}
else if (c == punc[0]) {
var str = value.Substring(0, value.IndexOf(".") + 1);
if (str.Length <= 30)
continue;
else
{
if (str.Length > maxLength)
{
continue;
}
else
{
return str;
}
}
}
}
var output = value.Length <= maxLength ? value : value.Substring(0, maxLength);
output = output.Substring(0, output.LastIndexOf(' '));
return output + "...";
}
}
Main program
public class Program
{
public static void Main()
{
string sentence = "This is a simple! question that ends with! a question mark. This is nearly the same. sentence that also ends with a period.";
string longSentence = "This is a simple question that does not end with a question mark This is nearly the same sentence that ends with a period.";
var output = StringExtensions.TruncateAtFirstSentence(sentence, 100);
var output2 = StringExtensions.TruncateAtFirstSentence(longSentence, 100);
Console.WriteLine("output 1: \n{0}\n", output);
Console.WriteLine("output 2: \n{0}\n", output2);
}
}