5

I have already taken a look at such posts like:

Format to first letter uppercase
How to capitalise the first letter of every word in a string

But none of these seem to actually work. I would have thought to start with that there would just be a:

.Capitalize();

Like there is:

.Lower(); & .Upper();

Are there any documentation or references regarding converting to a string like the following?

string before = "INVOICE";

To then becoming:

string after = "Invoice";

I receive no errors using the way the posts solutions I read give me, however, the before still remains capitalized.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    You can create a function that takes a string as input, takes the first character as a substring, makes that uppercase, replaces the first character with the uppercase version and returns the result. – Rob Feb 22 '16 at 09:22
  • 2
    And second question you need to read is [Convert all first letter to upper case, rest lower for each word](http://stackoverflow.com/questions/1943273/convert-all-first-letter-to-upper-case-rest-lower-for-each-word?lq=1) – Sergey Berezovskiy Feb 22 '16 at 09:25
  • 1
    `s = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(s.toLower());` from the so called duplicate is the link I provided in the question which I stated 'DID NOT WORK' so unmark duplicate, thanks... @SergeyBerezovskiy – Jaquarh Feb 22 '16 at 09:28
  • 1
    @KyleE4K 'DID NOT WORK` is not explanation of problem. Question is already answered and it has WORKING solution. Read both links carefully – Sergey Berezovskiy Feb 22 '16 at 09:30
  • 1
    So you have just admitted it isn't a duplicate, you're now saying the question does not state an explanation of the issue... make up your mind @SergeyBerezovskiy – Jaquarh Feb 22 '16 at 09:32
  • @KyleE4K you marked copy of solution I linked as correct answer. So it **is** duplicate as I thought. And you should go and read [How to ask questions](http://stackoverflow.com/help/how-to-ask). I'm pretty sure that you thought strings are mutable in .net – Sergey Berezovskiy Feb 22 '16 at 09:45
  • You changed the duplicate link, so lets not pretend – Jaquarh Feb 22 '16 at 10:09

2 Answers2

20

What about using ToUpper on the first char and ToLower on the remaining string?

string after = char.ToUpper(before.First()) + before.Substring(1).ToLower();
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • `string trantype = char.ToUpper(row.Trantype[0]) + row.Trantype.SubString(1).ToLower();` - Returns this error: 'string' does not contain a definition for 'SubString' and no extension method 'SubString' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) – Jaquarh Feb 22 '16 at 09:25
  • 3
    There is a typo: `SubString` is called `Substring`. – Patrick Hofman Feb 22 '16 at 09:25
  • 1
    If you remove the `]` after the `.First()` it works :) Thanks again! – Jaquarh Feb 22 '16 at 09:30
  • 1
    Yeah, my bad. Sorry. – Patrick Hofman Feb 22 '16 at 09:31
11

You can create a method that does something like this:

string UppercaseFirst(string str)
{
    if (string.IsNullOrEmpty(str))
        return string.Empty;
    return char.ToUpper(str[0]) + str.Substring(1).ToLower();
}

And use it like this:

string str = "thISstringLOokSHorribLE";
string upstr = UppercaseFirst(str);

to get this:

Thisstringlookshorrible
Ian
  • 30,182
  • 19
  • 69
  • 107