BestPractice - Transform first character of a string into lower case

前端 未结 11 2042

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

    I would use simple concatenation:

    Char.ToLowerInvariant(name[0]) + name.Substring(1)
    

    The first solution is not optimized because string.Format is slow and you don't need it if you have a format that will never change. It also generates an extra string to covert the letter to lowercase, which is not needed.

    The approach with "+ 32" is ugly / not maintainable as it requires knowledge of ASCII character value offsets. It will also generate incorrect output with Unicode data and ASCII symbol characters.

提交回复
热议问题