Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Character of String Lowercase - C# [duplicate]

How can I make the first character of a string lowercase?

For example: ConfigService

And I need it to be like this: configService

like image 760
nemo_87 Avatar asked Feb 13 '14 13:02

nemo_87


People also ask

How do you lowercase the first letter of a string?

To capitalize the first letter of a string and lowercase the rest, use the charAt() method to access the character at index 0 and capitalize it using the toUpperCase() method. Then concatenate the result with using the slice() method to to lowercase the rest of the string.

How do I convert a string to lowercase except first letter?

In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead.

How does tolower work in c?

The tolower() function takes an uppercase alphabet and convert it to a lowercase character. If the arguments passed to the tolower() function is other than an uppercase alphabet, it returns the same character that is passed to the function. It is defined in ctype.

Is c sharp Capitalize First letter?

In C#, the Toupper() function of the char class converts a character into uppercase. In the case that we will be discussing, only the first character of the string needs to be converted to uppercase; the rest of the string will stay as it is.


1 Answers

This will work:

public static string? FirstCharToLowerCase(this string? str)
{
    if ( !string.IsNullOrEmpty(str) && char.IsUpper(str[0]))
        return str.Length == 1 ? char.ToLower(str[0]).ToString() : char.ToLower(str[0]) + str[1..];

    return str;
}

(This code arranged as "C# Extension method")

Usage:

myString = myString.FirstCharToLowerCase();
like image 137
Amit Joki Avatar answered Oct 12 '22 23:10

Amit Joki