[ Taken from my post at the ms forums here: http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1014 ]
This is a bit bodged but I just hashed it together to see how easy it would be. I thought back and could have just iterated over chararray, but it works which is what counts :D
/// <summary>
/// Represents a paragraph in English
/// </summary>
public abstract class Paragraph
{
/// <summary>
/// Convert a string in arbitrary case to English sentence capitalisation.
/// </summary>
/// <param name="text">The text to convert</param>
/// <returns>The paragraph of text</returns>
public static string ToSentenceCase(string text)
{
string temporary = text.ToLower();
string result = "";
while (temporary.Length>0)
{
string[] splitTemporary = splitAtFirstSentence(temporary);
temporary = splitTemporary[1];
if (splitTemporary[0].Length>0)
{
result += capitaliseSentence(splitTemporary[0]);
}
else
{
result += capitaliseSentence(splitTemporary[1]);
temporary = "";
}
}
return result;
}
private static string capitaliseSentence(string sentence)
{
string result = "";
while (sentence[0]==' ')
{
sentence = sentence.Remove(0,1);
result+=" ";
}
if (sentence.Length>0)
{
result += sentence.TrimStart().Substring(0, 1).ToUpper();
result += sentence.TrimStart().Substring(1, sentence.TrimStart().Length-1);
}
return result;
}
private static string[] splitAtFirstSentence(string text)
{
//these are the characters to start a new sentence after
int lastChar = text.IndexOfAny(new char[] {'.', ':', '\\', '\r', '!', '?'})+1;
if (lastChar==1)
{
lastChar = 0;
}
return new string[] { text.Substring(0, lastChar), text.Substring(lastChar, text.Length-lastChar) };
}
}