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)
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)
Ran*_*ard 15
如果您使用 .NET Core 3 或 .NET 5,您可以调用:
System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(someString)
那么你肯定会得到与 ASP.NET 自己的 JSON 序列化器相同的结果。
这是我的代码,以防对任何人有用
// 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)
最后一行将第一个字符更改为大写,但您可以将其更改为小写或任何您喜欢的内容。
以下代码也适用于首字母缩略词。如果它是第一个单词,它会将首字母缩略词转换为小写(例如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)
示例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)
在 .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)
| 归档时间: |
|
| 查看次数: |
52944 次 |
| 最近记录: |