-4

How can I output this when I'm using Replace and ToUpper in visual studio c#

FirstName

Here's my code:

private void button1_Click(object sender, EventArgs e)
{
    string input;
    input = comboBox1.Text;
    input = input.Replace("_", "");
    label1.Text = input.First().ToString().ToUpper() 
                     + String.Join("", input.Skip(1));    
}

The output is always this:

Firstname

Sayse
  • 42,633
  • 14
  • 77
  • 146
Amped
  • 1
  • 1

2 Answers2

2

If the input is "first_name" then this works:

var text = "first_name";

text = String.Join("",
    text
        .Split('_')
        .Where(x => !String.IsNullOrEmpty(x))
        .Select(x => new string(
            x
                .Take(1)
                .Select(c => char.ToUpperInvariant(c))
                .Concat(x.Skip(1))
                .ToArray())));
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

This code below gets the first character (of type char) of input string then cast it to string and make that first letter upper case:

input.First().ToString() + ...

in next segment of your code you add up the rest of the string with emtpy string seporator with your first letter:

... + String.Join("", input.Skip(1))

So if you want to make all first letters upper case you should use LINQ provided by Enigmativity.