C# 将字符串从滴定酶C转换为camelCase#

C# 将字符串从滴定酶C转换为camelCase#,c#,camelcasing,tolower,title-case,C#,Camelcasing,Tolower,Title Case,我有一个字符串,我将其转换为TextInfo.ToTitleCase,删除了下划线并将字符串连接在一起。现在我需要将字符串中的第一个也是唯一的第一个字符改为小写,由于某种原因,我不知道如何完成它。提前谢谢你的帮助 class Program { static void Main(string[] args) { string functionName = "zebulans_nightmare"; TextInfo txtInfo = new Cu

我有一个字符串,我将其转换为TextInfo.ToTitleCase,删除了下划线并将字符串连接在一起。现在我需要将字符串中的第一个也是唯一的第一个字符改为小写,由于某种原因,我不知道如何完成它。提前谢谢你的帮助

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();
    }
}
结果:泽布兰斯噩梦

期望的结果:泽布兰斯梦魇

更新:

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

生成所需的输出

您只需降低数组中的第一个字符。看到这个了吗

作为旁注,在删除空格时,可以用空字符串替换下划线

.Replace("_", string.Empty)

这是我的代码,以防对任何人有用

    // 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.ToLower(x[0]) + x.Substring(1);
    }
如果您喜欢Pascal case,请使用:

    static string PascalCase(string s)
    {
        var x = CamelCase(s);
        return char.ToUpper(x[0]) + x.Substring(1);
    }

在扩展方法中实现了Bronumski的答案(不替换下划线)

例01

例02

例03

publicstaticstringtocamelcase(此字符串文本)
{
char[]a=text.ToLower().ToCharArray();
对于(int i=0;i
这应该通过使用系统来实现。全球化

改编自莱昂纳多的:


转换为PascalCase,方法是首先在任何大写字母前添加空格,然后在删除所有空格之前转换为标题大小写。

以下代码也适用于首字母缩略词。如果是第一个单词,则将首字母缩略词转换为小写(例如,
VATReturn
转换为
VATReturn
),否则保持原样(例如,
excludedwat
转换为
excludedwat


这是我的代码,包括降低所有的前缀:

公共静态类StringExtensions
{
公共静态字符串ToCamelCase(此字符串str)
{
bool hasValue=!string.IsNullOrEmpty(str);
//没有值或已使用大小写的单词
if(!hasValue | |(hasValue&&Char.IsLower(str[0]))
{
返回str;
}
字符串finalStr=“”;
int len=str.长度;
int-idx=0;
char nextChar=str[idx];
while(Char.IsUpper(nextChar))
{
finalStr+=char.ToLowerInvariant(nextChar);
if(len-1==idx)
{
//结束
打破
}
nextChar=str[++idx];
}
//如果不是字符串的结尾
if(idx!=len-1)
{
finalStr+=str.Substring(idx);
}
返回finalStr;
}
}
像这样使用它:

字符串camelcaseddb=“DOB”.ToCamelCase();

字符串是不可变的,但我们可以使用不安全的代码使其可变。 复制确保原始字符串保持原样

为了运行这些代码,您必须允许项目中存在不安全的代码

public静态不安全字符串ToCamelCase(此字符串值)
{
if(value==null | | value.Length==0)
{
返回值;
}
字符串结果=string.Copy(值);
已修复(char*chr=result)
{
char valueChar=*chr;
*chr=char.ToLowerInvariant(valueChar);
}
返回结果;
}
此版本修改原始字符串,而不是返回修改后的副本。这将是恼人的,但完全不常见。因此,请确保XML注释警告用户这一点

public静态不安全void ToCamelCase(此字符串值)
{
if(value==null | | value.Length==0)
{
返回值;
}
固定(字符*chr=值)
{
char valueChar=*chr;
*chr=char.ToLowerInvariant(valueChar);
}
返回值;
}

为什么要使用不安全的代码呢?简短的回答。。。这是我的代码,非常简单。我的主要目标是确保camel套管与ASP.NET序列化对象的内容兼容,而上面的示例并不能保证这一点

public static class StringExtensions
{
    public static string ToCamelCase(this string name)
    {
        var sb = new StringBuilder();
        var i = 0;
        // While we encounter upper case characters (except for the last), convert to lowercase.
        while (i < name.Length - 1 && char.IsUpper(name[i + 1]))
        {
            sb.Append(char.ToLowerInvariant(name[i]));
            i++;
        }

        // Copy the rest of the characters as is, except if we're still on the first character - which is always lowercase.
        while (i < name.Length)
        {
            sb.Append(i == 0 ? char.ToLowerInvariant(name[i]) : name[i]);
            i++;
        }

        return sb.ToString();
    }
}
公共静态类StringExtensions
{
公共静态字符串ToCamelCase(此字符串名称)
{
var sb=新的StringBuilder();
var i=0;
//当我们遇到大写字符(最后一个除外)时,将其转换为小写。
而(i
如果您使用的是.NET Core 3或.NET 5,您可以调用:

System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(someString)


然后您肯定会得到与ASP.NET自己的JSON序列化程序相同的结果。

谢谢我所需要的。打得好。进行了调整并更新了问题。这不适用于开头的首字母缩略词(例如,VATReturnAmount)。见。同意@Mojtaba。如果希望与ASP.NET中的camelCase JSON序列化兼容,请不要使用此选项。开头的首字母缩略词不一样。欢迎使用StackOverflow!虽然这可能会回答这个问题,考虑添加文本,为什么这是一个合适的答案和链接,以支持您的答案!考虑到没有其他答案有一篇文章支持这个答案,只有一篇有链接,我认为这有点苛刻,或者说很“过分”。肯尼给出的第一个答案是一个很好的回答(虽然我同意文本中有一个拼写错误)。这很苛刻吗?@CodeWarrior不,不是。他本来可以的
 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;
     }
 }

 //Or

 public static class StringExtension
 {
     public static string ToCamelCase(this string str) =>
         return string.IsNullOrEmpty(str) || str.Length < 2
         ? str
         : char.ToLowerInvariant(str[0]) + str.Substring(1);
 }
string input = "ZebulansNightmare";
string output = input.ToCamelCase();
    public static string ToCamelCase(this string text)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
    }
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);
    }
public static string CamelCase(this string str)  
    {  
      TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
      str = cultInfo.ToTitleCase(str);
      str = str.Replace(" ", "");
      return str;
    }
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;
}
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;
            });
var camelCaseFormatter = new JsonSerializerSettings();
camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();

JsonConvert.SerializeObject(object, camelCaseFormatter));
public static class StringExtensions
{
    public static string ToCamelCase(this string name)
    {
        var sb = new StringBuilder();
        var i = 0;
        // While we encounter upper case characters (except for the last), convert to lowercase.
        while (i < name.Length - 1 && char.IsUpper(name[i + 1]))
        {
            sb.Append(char.ToLowerInvariant(name[i]));
            i++;
        }

        // Copy the rest of the characters as is, except if we're still on the first character - which is always lowercase.
        while (i < name.Length)
        {
            sb.Append(i == 0 ? char.ToLowerInvariant(name[i]) : name[i]);
            i++;
        }

        return sb.ToString();
    }
}