BestPractice - Transform first character of a string into lower case

前端 未结 11 2060

I\'d like to have a method that transforms the first character of a string into lower case.

My approaches:

1.

public static string ReplaceFir         


        
11条回答
  •  孤街浪徒
    2020-12-04 12:31

    Just in case it helps anybody who happens to stumble across this answer.

    I think this would be best as an extension method, then you can call it with yourString.FirstCharacterToLower();

    public static class StringExtensions
    {
        public static string FirstCharacterToLower(this string str)
        {
            if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
            {
                return str;
            }
    
            return Char.ToLowerInvariant(str[0]) + str.Substring(1);
        }
    }
    

提交回复
热议问题