public static string
SentenceCase(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)
{
int
lastChar = text.IndexOfAny(new char[] { '.', ':', '\n', '\r', '!', '?' }) + 1;
if (lastChar
== 1)
{
lastChar = 0;
}
return new string[] {
text.Substring(0, lastChar),text.Substring(lastChar,
text.Length - lastChar) };
}
No comments:
Post a Comment