Search code examples
c#uppercaseletter

First letter capital in string c#


I am trying to change the first letter of a string to capital

I saw other questions on this but even when I apply what they've said I still can't manage the correct result.

public string FirstLetterToUpper(string str)
{
    if (str == null)
        return null;

    if (str.Length > 1)
        return char.ToUpper(str[0]) + str.Substring(1);

    return str.ToUpper();
}

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "test text";
    CapitalizeFirstLetter(label1.Text);        
}

Instead of outputting

Test text

it remains

test text

Any ideas?


Solution

  • You need to assign the result (and use the correct method name)

    label1.Text = FirstLetterToUpper("test text");