sentence case string

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!



Answer this question

sentence case string

  • PUG666

    Here, I just wrote this as this made me curious.  I know this could probably be done a lot better by iterating over a chararray and changing the case depending on flipping a bit to indicate it was necessary but I was lazy and used strings:

    /// <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...)



  • beigewy

    You would have to have something scan all of the text there... becuase he is not wanting it all to goto lower case just the this that does not start at the beg of the sentence... Hmm I wonder if you could use something like a language service or something or some sorta regex type thing to look over the string and then tek everything just after a period or a grammar mark and make it a capitial, and then make the rest lower or maybe you could take it and have it look for grammar marks and then take those letter and Hold there Upper case value in a temp string and then apply blah.ToLower() to the rest of the string and then return the conjoined value. I think either way you going to have to sep it out and have them look for the grammar marks, and ToTitleCase will not work because it does not apply grammar rules at all and just caps the first letter in each word.


  • Rajesh Prabhu. R

    give this KB a shot
    http://support.microsoft.com/kb/312890/EN-US/

    regards
    erymuzuan mustapa

  • Andreas Brosten

    You can use the string.ToLower(); method:

    // Init new string and convert it to a lower string.
    string myString = "HI ALL! I'M A NEWBIE. PLZ HELP!";
    myString = myString.ToLower();

  • sentence case string