BestPractice - Transform first character of a string into lower case

前端 未结 11 2021

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

    Combined a few and made it a chainable extension. Added short-circuit on whitespace and non-letter.

    public static string FirstLower(this string input) => 
        (!string.IsNullOrWhiteSpace(input) && input.Length > 0 
            && char.IsLetter(input[0]) && !char.IsLower(input[0]))
        ? input[0].ToString().ToLowerInvariant() + input.Remove(0, 1) : input;
    

提交回复
热议问题