最佳实践-将字符串的第一个字符转换为小写

时间:2022-08-04 09:26:56

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

我想要一个方法,把字符串的第一个字符转换成小写。

My approaches:

我的方法:

1.

1。

public static string ReplaceFirstCharacterToLowerVariant(string name)
{
    return String.Format("{0}{1}", name.First().ToString().ToLowerInvariant(), name.Substring(1));
}

2.

2。

public static IEnumerable<char> FirstLetterToLowerCase(string value)
{
    var firstChar = (byte)value.First();
    return string.Format("{0}{1}", (char)(firstChar + 32), value.Substring(1));
}

What would be your approach?

你的方法是什么?

7 个解决方案

#1


184  

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.

使用“+ 32”的方法很难看/不能维护,因为它需要了解ASCII字符值偏移量。它还将使用Unicode数据和ASCII符号生成不正确的输出。

#2


45  

Depending on the situation, a little defensive programming might be desirable:

根据具体情况,可能需要进行一些防御性的规划:

public static string FirstCharacterToLower(string str)
{
    if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
        return str;

    return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}

The if statement also prevents a new string from being built if it's not going to be changed anyway. You might want to have the method fail on null input instead, and throw an ArgumentNullException.

if语句还可以防止构建一个新的字符串,如果它不会被修改的话。您可能希望方法在null输入上失败,并抛出ArgumentNullException。

As people have mentioned, using String.Format for this is overkill.

正如人们提到的,使用字符串。这种格式太过了。

#3


5  

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);
    }
}

#4


3  

Mine is

我的是

if (!string.IsNullOrEmpty (val) && val.Length > 0)
{
    return val[0].ToString().ToLowerInvariant() + val.Remove (0,1);   
}

#5


2  

I like the accepted answer, but beside checking string.IsNullOrEmpty I would also check if Char.IsLower(name[1]) in case you are dealing with abbreviation. E.g. you would not want "AIDS" to become "aIDS".

我喜欢被接受的答案,但不喜欢检查字符串。IsNullOrEmpty我还将检查Char.IsLower(名称为[1])是否在处理缩写。你不会希望“艾滋病”变成“艾滋病”。

#6


0  

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;

#7


-2  

It is better to use String.Concat than String.Format if you know that format is not change data, and just concatenation is desired.

最好使用字符串。Concat比字符串。如果您知道格式不是更改数据的格式,并且只需要连接。

#1


184  

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.

使用“+ 32”的方法很难看/不能维护,因为它需要了解ASCII字符值偏移量。它还将使用Unicode数据和ASCII符号生成不正确的输出。

#2


45  

Depending on the situation, a little defensive programming might be desirable:

根据具体情况,可能需要进行一些防御性的规划:

public static string FirstCharacterToLower(string str)
{
    if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
        return str;

    return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}

The if statement also prevents a new string from being built if it's not going to be changed anyway. You might want to have the method fail on null input instead, and throw an ArgumentNullException.

if语句还可以防止构建一个新的字符串,如果它不会被修改的话。您可能希望方法在null输入上失败,并抛出ArgumentNullException。

As people have mentioned, using String.Format for this is overkill.

正如人们提到的,使用字符串。这种格式太过了。

#3


5  

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);
    }
}

#4


3  

Mine is

我的是

if (!string.IsNullOrEmpty (val) && val.Length > 0)
{
    return val[0].ToString().ToLowerInvariant() + val.Remove (0,1);   
}

#5


2  

I like the accepted answer, but beside checking string.IsNullOrEmpty I would also check if Char.IsLower(name[1]) in case you are dealing with abbreviation. E.g. you would not want "AIDS" to become "aIDS".

我喜欢被接受的答案,但不喜欢检查字符串。IsNullOrEmpty我还将检查Char.IsLower(名称为[1])是否在处理缩写。你不会希望“艾滋病”变成“艾滋病”。

#6


0  

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;

#7


-2  

It is better to use String.Concat than String.Format if you know that format is not change data, and just concatenation is desired.

最好使用字符串。Concat比字符串。如果您知道格式不是更改数据的格式,并且只需要连接。