帕斯卡案例的函数? (C#)
我需要一个函数,它将接受一个字符串并对其进行“帕斯卡大小写”。新单词开始的唯一指示符是下划线。以下是一些需要清理的示例字符串:
- price_old =>应该是 PriceOldrank_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);
}
上面的函数不做的事情是删除下划线和“ToUpper”下划线右侧的字符。
另外,关于如何对没有任何指示符(如下划线)的字符串进行帕斯卡大小写的任何想法。例如:
- companysourcefinancialtrendaccountingchangetype
- 。
- 这里的
主要挑战是确定一个单词在哪里结束而另一个单词在哪里开始 我想我需要某种查找词典来确定新单词从哪里开始?我们那里已经有图书馆可以做这类事情了吗?
谢谢,
保罗
I need a function that will take a string and "pascal case" it. The only indicator that a new word starts is an underscore. Here are some example strings that need to be cleaned up:
- price_old => Should be PriceOld
- rank_old => Should be RankOld
I started working on a function that makes the first character upper case:
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);
}
The thing the above function doesn't do is remove the underscore and "ToUpper" the character to the right of the underscore.
Also, any ideas about how to pascal case a string that doesn't have any indicators (like the underscore). For example:
- companysource
- financialtrend
- accountingchangetype
The major challenge here is determining where one word ends and another starts. I guess I would need some sort of lookup dictionary to determine where new words start? Are there libraries our there to do this sort of thing already?
Thanks,
Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 TextInfo.ToTitleCase 方法删除“_”字符。
因此,使用我拥有的扩展方法:
http:// theburningmonk.com/2010/08/dotnet-tips-string-totitlecase-extension-methods
你可以这样做:
You can use the TextInfo.ToTitleCase method then remove the '_' characters.
So, using the extension methods I've got:
http://theburningmonk.com/2010/08/dotnet-tips-string-totitlecase-extension-methods
you can do somethingl ike this:
嗯,第一件事很简单:
返回
PriceOld
第二件事要困难得多。由于
companysource
可能是CompanySource
或CompanysOurce
,可以自动化,但非常错误。您将需要一本英语词典,并进行一些猜测(啊好吧,我的意思是很多)哪个单词组合是正确的。Well the first thing is easy:
returns
PriceOld
Second thing is way more difficult. As
companysource
could beCompanySource
or maybeCompanysOurce
, can be automated but is quite faulty. You will need an English dictionary, and do some guessing (ah well, I mean alot) on which combination of words is correct.试试这个:
Try this:
带下划线:
不带下划线:
你就得靠你自己了。但继续寻找吧;如果之前没有人这样做过,我会感到惊讶。
With underscores:
Without underscores:
You're on your own there. But go ahead and search; I'd be surprised if nobody has done this before.
对于拆分连接单词的第二个问题,您可以利用我们最好的朋友 Google 和 Google。如果您的串联输入由常用的英语单词组成,则搜索引擎对单个单词作为替代搜索查询的命中率很高。
如果您输入示例输入,Google 和 Bing 会建议以下内容:
请参阅 此示例。
为此编写一个小屏幕抓取工具应该相当容易。
For your 2nd problem of splitting concatenated words, you could utilize our best friends Google & Co. If your concatenated input is made up of usual english words, the search engines have a good hit rate for the single words as an alternative search query
If you enter your sample input, Google and Bing suggest the following:
See this exaple.
Writing a small screen scraper for that should be fairly easy.
对于那些需要非正则表达式解决方案的人
for those who needs a non regex solution