How to convert uppercase string to sentence case string in C# e.g.
input:
HI ALL! I'M A NEWBIE. PLZ HELP!
output:
Hi all! I'm a newbie. Plz help!
How to convert uppercase string to sentence case string in C# e.g.
input:
HI ALL! I'M A NEWBIE. PLZ HELP!
output:
Hi all! I'm a newbie. Plz help!
sentence case string
magruder2
http://support.microsoft.com/kb/312890/EN-US/
regards
erymuzuan mustapa
ChandraW
/// <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[] {'.', ':', '\n', '\r', '!', ' '})+1;
if (lastChar==1)
{
lastChar = 0;
}
return new string[] { text.Substring(0, lastChar), text.Substring(lastChar, text.Length-lastChar) };
}
}
(Apologies for the formatting, it seem that freetextbox has messed up the tabs and carriage returns...)
Mark_A_Polson
// Init new string and convert it to a lower string.
string myString = "HI ALL! I'M A NEWBIE. PLZ HELP!";
myString = myString.ToLower();
Curt Zarger