Search Unity

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

Discussion in 'Scripting' started by tomowale, Oct 15, 2021.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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 ?

    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using System;
    6. public class NameEnter : MonoBehaviour // Script to handle the player manually typing in their name
    7. {
    8.      public TMP_InputField firstNameField;
    9.      public TMP_InputField middleNameField;
    10.      public TMP_InputField LastNameField;
    11.      
    12.      public void EnterFirstName()
    13.                              
    14.      {
    15.          MainPlayer.Instance.playerFirstName = firstNameField.text;
    16.      }
     
  2. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    I‘m not at the PC at the moment but can‘t you access string[0]?
     
  3. Deleted User

    Deleted User

    Guest

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

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I could, but even with accessing it, I couldn't just use ToUpper() on that for whatever reason
     
  5. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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.
     
  6. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    I'm certain this is bad in many ways but... it does the trick:

    Code (CSharp):
    1.         tempstring = string1[0].ToString();
    2.         tempstring=tempstring.ToUpper();
    3.         string1 = string1.Remove(0, 1);
    4.         string1 = string1.Insert(0, tempstring);
    5.         Debug.Log(string1);
     
  7. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    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.
     
  8. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
  9. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Code (csharp):
    1.  
    2. string output = input.Substring(0, 1).ToUpper() + input.Substring(1).ToLower();
    3.  
    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.
     
  10. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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.
     
  11. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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.

    https://www.tutorialsrack.com/artic...rst-letter-of-each-word-in-a-string-in-csharp
     
  12. Akidamaru

    Akidamaru

    Joined:
    Sep 8, 2015
    Posts:
    7
    I really dont like all these ifs but this could be a solution:

    Code (CSharp):
    1. public string ForceFirstUpper(string word)
    2.         {
    3.             if(word.Length == 0)
    4.             {
    5.                 return word;
    6.             }      
    7.             if(word.Length < 2)
    8.             {
    9.                 return word.ToUpper();
    10.             }
    11.             return string.Concat(word[0].ToString().ToUpper(), word.Substring(1));
    12.         }
    You could add a check if the first letter is already upper case between the 2 ifs

    I really like @tomowale solution tho
     
    Last edited: Mar 16, 2023
    bravophantom likes this.
  13. ahmararif

    ahmararif

    Joined:
    May 12, 2023
    Posts:
    2
    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"
     
    Hugo-ElectricMonkeys likes this.