Improve the camel string method

Possible duplicate:
String conversion in C #

I want a "camelize" string, for example:

  • PARTS / ACCESSORIES โ†’ Parts / Accessories
  • HELLO WORLD / TEST โ†’ Hello World / Test
  • Hello World โ†’ Hello World

Here is what I still have:

  public static string Camelize(this string str)
    {
        if (String.IsNullOrEmpty(str)) return "";

        var sb = new StringBuilder();
        char[] chars = str.ToLower().ToCharArray();
        bool upper = true;            
        // ' ', '-', '.', '/'
        for (int i = 0; i < chars.Length; ++i)
        {
            char c = chars[i];
            if (i == 0 || //First char 
                chars[i - 1] == ' ' ||
                chars[i - 1] == '-' ||
                chars[i - 1] == '.' ||
                chars[i - 1] == '/'
                ) upper = true;

            if (upper)             
                sb.Append(Char.ToUpper(c));                
            else                
                sb.Append(c);

            upper = false;
        }
        return sb.ToString();
    }

Is there any way to improve this method, also I know that the lines will not exceed 250 characters? Thanks

+3
source share
1 answer

What about:

    public static string Camelize(string text)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());
    }
+14
source

Source: https://habr.com/ru/post/1751668/


All Articles