using System.Collections.Generic;
using System.Threading.Tasks;
public static class StringExtensions
public static string TruncateAtFirstSentence(this string value, int maxLength)
char[] punc = new char[3];
foreach (char c in value) {
var str = value.Substring(0, value.IndexOf("?") + 1);
if (str.Length > maxLength)
var str = value.Substring(0, value.IndexOf("!") + 1);
if (str.Length > maxLength)
var str = value.Substring(0, value.IndexOf(".") + 1);
if (str.Length > maxLength)
var output = value.Length <= maxLength ? value : value.Substring(0, maxLength);
output = output.Substring(0, output.LastIndexOf(' '));
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 sort of 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);