从TitleCase C#将字符串转换为camelCase [英] Convert String To camelCase from TitleCase C#

查看:482
本文介绍了从TitleCase C#将字符串转换为camelCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,我将其转换为TextInfo.ToTitleCase并删除了下划线并将该字符串连接在一起.现在,我需要将字符串中的第一个字符和只有第一个字符更改为小写,并且由于某种原因,我不知道如何完成此操作.预先感谢您的帮助.

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

结果:Zebulans噩梦

Results: ZebulansNightmare

期望的结果:斑马梦ight

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

产生所需的输出

推荐答案

您只需要降低数组中的第一个字符.参见此 answer

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)

这篇关于从TitleCase C#将字符串转换为camelCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆