Convert String To camelCase from TitleCase C#

后端 未结 11 1496

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:57

    Example 01

        public static string ToCamelCase(this string text)
        {
            return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
        }
    

    Example 02

    public static string ToCamelCase(this string text)
        {
            return string.Join(" ", text
                                .Split()
                                .Select(i => char.ToUpper(i[0]) + i.Substring(1)));
        }
    

    Example 03

        public static string ToCamelCase(this string text)
        {
            char[] a = text.ToLower().ToCharArray();
    
            for (int i = 0; i < a.Count(); i++)
            {
                a[i] = i == 0 || a[i - 1] == ' ' ? char.ToUpper(a[i]) : a[i];
    
            }
            return new string(a);
        }
    

提交回复
热议问题