制作Pascal案例的功能?(C#)

Pau*_*yer 14 c# regex camelcasing

我需要一个带字符串和"pascal case"的函数.新单词开始的唯一指标是下划线.以下是一些需要清理的示例字符串:

  1. price_old =>应该是PriceOld
  2. rank_old =>应该是RankOld

我开始研究一个使第一个字符为大写的函数:

public string FirstCharacterUpper(string value)
{
 if (value == null || value.Length == 0)
  return string.Empty;
 if (value.Length == 1)
  return value.ToUpper();
 var firstChar = value.Substring(0, 1).ToUpper();
 return firstChar + value.Substring(1, value.Length - 1);
}
Run Code Online (Sandbox Code Playgroud)

上面的函数没有做的是删除下划线和"ToUpper"字符右下角.

此外,任何有关如何使用没有任何指标(如下划线)的字符串的概念.例如:

  1. companysource
  2. financialtrend
  3. accountingchangetype

这里的主要挑战是确定一个词的结束和另一个词的开始.我想我需要某种查找字典来确定新单词的起源位置?我们那里有图书馆可以做这种事吗?

谢谢,

保罗

the*_*onk 23

您可以使用TextInfo.ToTitleCase方法,然后删除'_'字符.

所以,使用我得到的扩展方法:

http://theburningmonk.com/2010/08/dotnet-tips-string-totitlecase-extension-methods

你可以做点什么:

var s = "price_old";
s.ToTitleCase().Replace("_", string.Empty);
Run Code Online (Sandbox Code Playgroud)


Jan*_*oom 11

那么第一件事很简单:

string.Join("", "price_old".Split(new [] { '_' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Substring(0, 1).ToUpper() + s.Substring(1)).ToArray());
Run Code Online (Sandbox Code Playgroud)

回报 PriceOld

第二件事情更难.作为companysource可能是CompanySource或可能CompanysOurce,可实现自动化,但相当错误的.你需要一本英文字典,然后做一些猜测(好吧,我的意思是很多)哪个词的组合是正确的.