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;
}
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.
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:
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.
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”