Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change string into first letter Uppercase, the rest of the string to lower case

Discussion in 'Scripting' started by Kaimukas, Mar 8, 2022.

  1. Kaimukas

    Kaimukas

    Joined:
    Mar 19, 2019
    Posts:
    30
    Hey, I've figured out how to change the first letter of a string into a capital letter, but how would I go around to keep the rest of the string lower case? For example:

    "dEFiNiTiOn" --> "Definition"

    I used this to change the first letter to upper case:

    Code (CSharp):
    1. if(str.Length == 0){
    2.             return;
    3.         }else if(str.Length == 1){
    4.             char.ToUpper(str[0]);
    5.         }else{
    6.             str = char.ToUpper(str[0]) + str.Substring(1);
    7.         }
    }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Change the whole string with ToLower, then just the first character with ToUpper. Or just use str.Substring(1).ToLower()
     
    Kaimukas and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    Here are two implementations, one is thread safe but will allocate more memory, the other is not thread safe but only allocates the new string by reusing the string builder:

    Code (CSharp):
    1. using System.Text;
    2.                    
    3. public static class StringExt
    4. {
    5.     // thread safe
    6.     public static string ToPascalCase_Safe(this string aText)
    7.     {
    8.         if (aText == null || aText.Length < 1)
    9.             return aText;
    10.         StringBuilder sb = new StringBuilder(aText);
    11.         sb[0] = char.ToUpper(sb[0]);
    12.         for(int i = 1; i < sb.Length; i++)
    13.             sb[i] = char.ToLower(sb[i]);
    14.         return sb.ToString();
    15.     }
    16.    
    17.     // not thread safe, but reduces garbage
    18.     private static StringBuilder m_SB = new StringBuilder();
    19.     public static string ToPascalCase(this string aText)
    20.     {
    21.         if (aText == null || aText.Length < 1)
    22.             return aText;
    23.         m_SB.Clear();
    24.         m_SB.Append(char.ToUpper(aText[0]));
    25.         for(int i = 1; i < aText.Length; i++)
    26.             m_SB.Append(char.ToLower(aText[i]));
    27.         return m_SB.ToString();
    28.     }
    29. }
    30.  
    Both are extension methods. So you can simply do

    Code (CSharp):
    1. str = str.ToPascalCase();
    To replace the original string with the converted string.
     
  4. Kaimukas

    Kaimukas

    Joined:
    Mar 19, 2019
    Posts:
    30
    Super simple, feel stupid for not thinking of that, thank you
     
  5. Chris-Ender

    Chris-Ender

    Joined:
    May 23, 2015
    Posts:
    12
    Last edited: Dec 7, 2022