BestPractice - Transform first character of a string into lower case

前端 未结 11 2041

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

    If you don't want to reference your string twice in your expression you could do this using System.Linq.

    new string("Hello World".Select((c, i) => i == 0 ? char.ToLower(c) : c).ToArray())
    

    That way if your string comes from a function, you don't have to store the result of that function.

    new string(Console.ReadLine().Select((c, i) => i == 0 ? char.ToLower(c) : c).ToArray())
    

提交回复
热议问题