How do I capatalize the first character (letter) of a string?

I posted this on the Answers section, but posting here just in case someone here knows the answer.

So I’ve got this input field in my game, once the player enters something into the input field, it is saved to a string variable. However, if the player was to enter all lowercase letters the first letter would not be capitalized. How would I go about doing this? ToUpper() capitalizes the entire string, is there a way to only capitalize the first letter of said string?

I’ve done a little research and haven’t really come across anything relevant for some reason (or perhaps there is some namespace I should be using I’m unaware of)

Here’s the code.

How would I capitalize the first letter of the firstNameField.text ?

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
public class NameEnter : MonoBehaviour // Script to handle the player manually typing in their name
{
     public TMP_InputField firstNameField;
     public TMP_InputField middleNameField;
     public TMP_InputField LastNameField;
     
     public void EnterFirstName()
                              
     {
         MainPlayer.Instance.playerFirstName = firstNameField.text;
     }

I‘m not at the PC at the moment but can‘t you access string[0]?

Your question is valid; knowing the answer would be useful, but what if people do not want capital letters in their name? :slight_smile:

I could, but even with accessing it, I couldn’t just use ToUpper() on that for whatever reason

Good point, I suppose it’s an aesthetic thing for me. Just having each letter of (each part of someone’s name) be capitalized, typically we do it in real life.

I’m certain this is bad in many ways but… it does the trick:

        tempstring = string1[0].ToString();
        tempstring=tempstring.ToUpper();
        string1 = string1.Remove(0, 1);
        string1 = string1.Insert(0, tempstring);
        Debug.Log(string1);

And for localization, capital letters are not a thing for every language. Even if it was, not every language reads left to right either, so knowing which end to begin from for capitalization is also a problem.

By the way, I’d like to add that ToUpper is capable of doing all this automatically according to docs. But I really don’t understand the documentation. Maybe you do:

string output = input.Substring(0, 1).ToUpper() + input.Substring(1).ToLower();

Probably not the most performant solution since this makes three strings during the calculations, but good enough. Remove the ToLower() if you don’t want to force the rest of the word to be lowercase.

This is something I hadn’t even thought of, definitely will keep it in mind. Though, English being my primary language, I’ll prioritize it in the meanwhile.

Someone over on Unity Answers linked me this, which ended up working. I just followed the documentation and used the System.Globalization namespace. There are some good answers here too, so if anything goes wrong. I think I could probably put GroZZleR’s/matzomat’s code into a return method and have it work. So, thank you guys!

Here’s the documentation for the code I used. It’s using the ToTitleCase method in the Globalization namespace.

I really dont like all these ifs but this could be a solution:

public string ForceFirstUpper(string word)
        {
            if(word.Length == 0)
            {
                return word;
            }      
            if(word.Length < 2)
            {
                return word.ToUpper();
            }
            return string.Concat(word[0].ToString().ToUpper(), word.Substring(1));
        }

You could add a check if the first letter is already upper case between the 2 ifs

I really like @tomowale solution tho

1 Like

Here is how you can do it!

using System.Globalization;
string originalString = “hello world”;
TextInfo textInfo = new CultureInfo(“en-US”, false).TextInfo;
string capitalizedString = textInfo.ToTitleCase(originalString);
// capitalizedString will now be “Hello World”

1 Like