1

How can I convert a string to a sentence case?

I don't want to convert to title case. My requirement is to convert the string to sentence case.

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
nimi
  • 5,359
  • 16
  • 58
  • 90
  • With regard to using jQuery: http://stackoverflow.com/questions/1504638/how-to-give-sentence-case-to-sentences-through-css-or-javascript – Marcus Whybrow Jan 13 '11 at 10:18
  • possible duplicate of http://stackoverflow.com/questions/3141426/net-method-to-convert-a-string-to-sentence-case – Pauli Østerø Jan 13 '11 at 10:23
  • @Pauli: Vote to close in such case – abatishchev Jan 13 '11 at 10:24
  • @Pauli:Sentence case in a general sense describes the way that capitalization is used within a sentence. Sentence case is also the capitalization of an English sentence, i.e. the first letter of the sentence is capitalized, with the rest being lower case (unless requiring capitalization for a specific reason, e.g. proper nouns, acronyms, etc.). – nimi Jan 13 '11 at 10:28
  • @Nimesh so it the requirement with or without proper capitalization for specific reasons? – Pauli Østerø Jan 13 '11 at 10:30
  • @pauli: it would be great if it's with proper capitalization. – nimi Jan 13 '11 at 10:38

2 Answers2

1

In C# you do this:

static string UppercaseFirst(string s)
{
    // Check for empty string.
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }
    // Return char and concat substring.
    return char.ToUpper(s[0]) + s.Substring(1);
}

In CSS you could do the following (if your browser supports it)

#mytitle:first-letter 
{
text-transform:capitalize;
}
Chris
  • 26,744
  • 48
  • 193
  • 345
0

I would vonvert the whole string to smallcase and then convert the first letter to upper.Heres an example in C#

string s = "SOME STRING";
System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
s.ToLower();
s.ToUpper(s.Substring(0, 1));
fARcRY
  • 2,338
  • 6
  • 24
  • 40
  • technically sentence case also takes special words nouns into account and capitalize them as well. – Pauli Østerø Jan 13 '11 at 10:22
  • Then you would have to have a list of 'special' words and then they can be capitalized as well, it depends how much time you want to spend on the problem. – fARcRY Jan 13 '11 at 10:24