Convert String To camelCase from TitleCase C#

后端 未结 11 1635

I have a string that I converted to a TextInfo.ToTitleCase and removed the underscores and joined the string together. Now I need to change the first and only the first charact

11条回答
  •  遇见更好的自我
    2021-02-04 23:40

    Implemented Bronumski's answer in an extension method (without replacing underscores).

     public static class StringExtension
     {
         public static string ToCamelCase(this string str)
         {                    
             if(!string.IsNullOrEmpty(str) && str.Length > 1)
             {
                 return char.ToLowerInvariant(str[0]) + str.Substring(1);
             }
             return str;
         }
     }
    
     //Or
    
     public static class StringExtension
     {
         public static string ToCamelCase(this string str) =>
             return string.IsNullOrEmpty(str) || str.Length < 2
             ? str
             : char.ToLowerInvariant(str[0]) + str.Substring(1);
     }
    

    and to use it:

    string input = "ZebulansNightmare";
    string output = input.ToCamelCase();
    

提交回复
热议问题