views:

94

answers:

3

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

My approaches:

1.

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

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?

+6  A: 

I would use this:

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

Your first solution is not optimized: string.Format is slow and you don't need it if you have a format that will never change.

The second is ugly and not maintainable.

onof
i would do it: `char.ToLower(name[0]).ToString() + name.Substring(1)`
Andrey
yes, i was just updating my answer, thanks
onof
I thought that concatination of strings with the + operator is slow and ugly, isn't it?
Rookian
Actually, getting rid of that conversion makes it use a slightly slower version of `String.Concat` :)
Thorarin
@Rookian: the `+` operator is slow when you are concatenating lots of strings. In that case a `StringBuilder` would perform much better. However, `+` is much faster than `string.Format`. Use the latter when you actually need to format something (like displaying integers, doubles or dates).
0xA3
@0x03: it's only slow if you're concatenating lots of strings iteratively. If you concatenate them all in a single operation, the `+` operator is not slow at all, because the compiler turns it into a `String.Concat` (however `String.Join` is faster than `String.Concat` for some silly reason).
Thorarin
A: 

Mine is

if (!string.IsNullOrEmpty (val) && val.Lenght >0)
{
    return val[0].ToString().ToLowerInvariant() + val.Remove (0,1);   
}
B-Rain
I'm curious, why the `val.Remove`? Seems a little counter-intuitive to me.
Thorarin
+3  A: 

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]).ToString() + 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.

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

Thorarin
+1 for checking if you actually need to do anything at all. :)
Chris
Correct me if I'm wrong butstr.Substring(1) will return the symbol at position 1 as the count for this method is not indicated.so you will have char[0] in lower case + the char at position 1So I preferred to remove one char starting from first char in the string. The result is the string without first letter. Then i will add this string to first char that is converted to lower case
B-Rain
@B-Rain: consider yourself corrected: http://msdn.microsoft.com/en-us/library/hxthx5h6%28VS.90%29.aspx
Thorarin
Ups. Good point, thanks
B-Rain