将字符串转换为来自TitleCase C#的camelCase

Gab*_*l_W 38 c# title-case camelcasing tolower

我有一个字符串,我转换为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();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:ZebulansNightmare

期望的结果:zebulansNightmare

更新:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

产生所需的输出

Bro*_*ski 66

您只需要降低数组中的第一个char.看到这个答案

Char.ToLowerInvariant(name[0]) + name.Substring(1)
Run Code Online (Sandbox Code Playgroud)

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

.Replace("_", string.Empty)
Run Code Online (Sandbox Code Playgroud)

  • 这不适用于开头的缩写词(例如,VATReturnAmount)。请参阅[我的答案](/sf/answers/4119751401/)。 (4认同)
  • 在 .NET Core C#9 中,您可以使其更加简洁:`return char.ToLowerInvariant(s[0]) + s[1..];` (3认同)
  • 同意@Mojtaba。如果您想与 ASP.NET 中的驼峰命名法 JSON 序列化兼容,请不要使用此选项。开头的缩写词不会一样。 (2认同)

Fab*_*ler 18

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

 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;
     }
 }
Run Code Online (Sandbox Code Playgroud)

并使用它:

string input = "ZebulansNightmare";
string output = input.ToCamelCase();
Run Code Online (Sandbox Code Playgroud)

  • 我看到的唯一问题是字符串是否只是一个"A".它应该返回"a",对吗? (2认同)

Ran*_*ard 15

如果您使用 .NET Core 3 或 .NET 5,您可以调用:

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

那么你肯定会得到与 ASP.NET 自己的 JSON 序列化器相同的结果。

  • 请注意,这不会删除空格,因此您需要执行 JsonNamingPolicy.CamelCase.ConvertName(str).Replace(" ", string.Empty) (3认同)
  • 这完全符合预期。 (2认同)

Joh*_*kel 8

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

    // 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);
    }
Run Code Online (Sandbox Code Playgroud)

最后一行将第一个字符更改为大写,但您可以将其更改为小写或任何您喜欢的内容。

  • 最后是一个将MYCase转换为myCase而不是mYCase的示例。谢谢! (3认同)

Moj*_*aba 8

以下代码也适用于首字母缩略词。如果它是第一个单词,它会将首字母缩略词转换为小写(例如VATReturnto vatReturn),否则保持原样(例如ExcludedVATto 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;
            });
Run Code Online (Sandbox Code Playgroud)


Ken*_*anp 7

示例01

    public static string ToCamelCase(this string text)
    {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text);
    }
Run Code Online (Sandbox Code Playgroud)

示例02

public static string ToCamelCase(this string text)
    {
        return string.Join(" ", text
                            .Split()
                            .Select(i => char.ToUpper(i[0]) + i.Substring(1)));
    }
Run Code Online (Sandbox Code Playgroud)

实施例03

    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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到 StackOverflow!尽管这可能会回答问题,但请考虑添加文本来说明为什么这是一个合适的答案以及支持您的答案的链接! (5认同)
  • 那很严厉吗? (3认同)
  • 最重要的是,它甚至没有回答问题。它转换为 TitleCase,这不是提出的问题。 (2认同)

Xav*_*ohn 5

在 .Net 6 及更高版本中

public static class CamelCaseExtension
{
  public static string ToCamelCase(this string str) => 
     char.ToLowerInvariant(str[0]) + str[1..];
}
Run Code Online (Sandbox Code Playgroud)