Convert String To camelCase from TitleCase C#

爷,独闯天下 提交于 2019-12-04 14:59:54

问题


I have a string that I converted to a TextInfo.ToTitleCase and removed the underscores and joined the string together. Now I need to change the first and only the first character in the string to lower case and for some reason, I can not figure out how to accomplish it. Thanks in advance for the help.

class Program
{
    static void Main(string[] args)
    {
        string functionName = "zebulans_nightmare";
        TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
        functionName = txtInfo.ToTitleCase(functionName).Replace('_', ' ').Replace(" ", String.Empty);
        Console.Out.WriteLine(functionName);
        Console.ReadLine();
    }
}

Results: ZebulansNightmare

Desired Results: zebulansNightmare

UPDATE:

class Program
{
    static void Main(string[] args)
    {
        string functionName = "zebulans_nightmare";
        TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
        functionName = txtInfo.ToTitleCase(functionName).Replace("_", string.Empty).Replace(" ", string.Empty);
        functionName = $"{functionName.First().ToString().ToLowerInvariant()}{functionName.Substring(1)}";
        Console.Out.WriteLine(functionName);
        Console.ReadLine();
    }
}

Produces the desired output


回答1:


You just need to lower the first char in the array. See this answer

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

As a side note, seeing as you are removing spaces you can replace the underscore with an empty string.

.Replace("_", string.Empty)



回答2:


Implemented Bronumski's answer in an extension method (without replacing underscores).

 public static class StringExtension
 {
     public static string ToCamelCase(this string str)
     {                    
         if(!string.IsNullOrEmpty(str) && str.Length > 1)
         {
             return Char.ToLowerInvariant(str[0]) + str.Substring(1);
         }
         return str;
     }
 }

and to use it:

string input = "ZebulansNightmare";
string output = input.ToCamelCase();



回答3:


Here is my code, in case it is useful to anyone

    // This converts to camel case
    // Location_ID => LocationId, and testLEFTSide => TestLeftSide

    static string CamelCase(string s)
    {
        var x = s.Replace("_", "");
        if (x.Length == 0) return "Null";
        x = Regex.Replace(x, "([A-Z])([A-Z]+)($|[A-Z])",
            m => m.Groups[1].Value + m.Groups[2].Value.ToLower() + m.Groups[3].Value);
        return char.ToUpper(x[0]) + x.Substring(1);
    }

The last line changes first char to uppercase, but you can change to lowercase, or whatever you like.




回答4:


public static string ToCamelCase(this string text)
{
    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(te);
}

public static string ToCamelCase(this string text)
{
    return String.Join(" ", text.Split()
    .Select(i => Char.ToUpper(i[0]) + i.Substring(1)));}

public static string ToCamelCase(this string text) {
  char[] a = text.ToLower().ToCharArray();

    for (int i = 0; i < a.Count(); i++ )
    {
        a[i] = i == 0 || a[i-1] == ' ' ? char.ToUpper(a[i]) : a[i];

    }
    return new string(a);}



回答5:


public static string CamelCase(this string str)  
    {  
      TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
      str = cultInfo.ToTitleCase(str);
      str = str.Replace(" ", "");
      return str;
    }

This should work using System.Globalization




回答6:


The following code works with acronyms as well. If it is the first word it converts the acronym to lower case (e.g., VATReturn to vatReturn), and otherwise leaves it as it is (e.g., ExcludedVAT to excludedVAT).

name = Regex.Replace(name, @"([A-Z])([A-Z]+|[a-z0-9_]+)($|[A-Z]\w*)",
            m =>
            {
                return m.Groups[1].Value.ToLower() + m.Groups[2].Value.ToLower() + m.Groups[3].Value;
            });



回答7:


Adapted from Leonardo's answer:

static string PascalCase(string str) {
  TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
  str = Regex.Replace(str, "([A-Z]+)", " $1");
  str = cultInfo.ToTitleCase(str);
  str = str.Replace(" ", "");
  return str;
}

Converts to PascalCase by first adding a space before any group of capitals, and then converting to title case before removing all the spaces.



来源:https://stackoverflow.com/questions/42310727/convert-string-to-camelcase-from-titlecase-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!