帕斯卡案例的函数? (C#)

发布于 2024-09-12 06:31:32 字数 775 浏览 7 评论 0原文

我需要一个函数,它将接受一个字符串并对其进行“帕斯卡大小写”。新单词开始的唯一指示符是下划线。以下是一些需要清理的示例字符串:

  1. price_old =>应该是 PriceOldrank_old
  2. =>应该是 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”下划线右侧的字符。

另外,关于如何对没有任何指示符(如下划线)的字符串进行帕斯卡大小写的任何想法。例如:

  1. companysourcefinancialtrendaccountingchangetype
  2. 这里的

主要挑战是确定一个单词在哪里结束而另一个单词在哪里开始 我想我需要某种查找词典来确定新单词从哪里开始?我们那里已经有图书馆可以做这类事情了吗?

谢谢,

保罗

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:

  1. price_old => Should be PriceOld
  2. 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:

  1. companysource
  2. financialtrend
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜清冷一曲。 2024-09-19 06:31:32

您可以使用 TextInfo.ToTitleCase 方法删除“_”字符。

因此,使用我拥有的扩展方法:

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

你可以这样做:

var s = "price_old";
s.ToTitleCase().Replace("_", string.Empty);

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:

var s = "price_old";
s.ToTitleCase().Replace("_", string.Empty);
情释 2024-09-19 06:31:32

嗯,第一件事很简单:

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

返回 PriceOld

第二件事要困难得多。由于 companysource 可能是 CompanySourceCompanysOurce,可以自动化,但非常错误。您将需要一本英语词典,并进行一些猜测(啊好吧,我的意思是很多)哪个单词组合是正确的。

Well the first thing is easy:

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

returns PriceOld

Second thing is way more difficult. As companysource could be CompanySource or maybe CompanysOurce, 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.

口干舌燥 2024-09-19 06:31:32

试试这个:

public static string GetPascalCase(string name)
{
    return Regex.Replace(name, @"^\w|_\w", 
        (match) => match.Value.Replace("_", "").ToUpper());
}

Console.WriteLine(GetPascalCase("price_old")); // => Should be PriceOld
Console.WriteLine(GetPascalCase("rank_old" )); // => Should be RankOld

Try this:

public static string GetPascalCase(string name)
{
    return Regex.Replace(name, @"^\w|_\w", 
        (match) => match.Value.Replace("_", "").ToUpper());
}

Console.WriteLine(GetPascalCase("price_old")); // => Should be PriceOld
Console.WriteLine(GetPascalCase("rank_old" )); // => Should be RankOld
樱花坊 2024-09-19 06:31:32

带下划线:

s = Regex.Replace(s, @"(?:^|_)([a-z])",
      m => m.Groups[1].Value.ToUpper());

不带下划线:

你就得靠你自己了。但继续寻找吧;如果之前没有人这样做过,我会感到惊讶。

With underscores:

s = Regex.Replace(s, @"(?:^|_)([a-z])",
      m => m.Groups[1].Value.ToUpper());

Without underscores:

You're on your own there. But go ahead and search; I'd be surprised if nobody has done this before.

三生路 2024-09-19 06:31:32

对于拆分连接单词的第二个问题,您可以利用我们最好的朋友 Google 和 Google。如果您的串联输入由常用的英语单词组成,则搜索引擎对单个单词作为替代搜索查询的命中率很高。

如果您输入示例输入,Google 和 Bing 会建议以下内容:

original             | Google                | Bing
=====================================================================
companysource        | company source        | company source 
financialtrend       | financial trend       | financial trend
accountingchangetype | accounting changetype | accounting change type

请参阅 此示例

为此编写一个小屏幕抓取工具应该相当容易。

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:

original             | Google                | Bing
=====================================================================
companysource        | company source        | company source 
financialtrend       | financial trend       | financial trend
accountingchangetype | accounting changetype | accounting change type

See this exaple.

Writing a small screen scraper for that should be fairly easy.

零崎曲识 2024-09-19 06:31:32

对于那些需要非正则表达式解决方案的人

 public static string RemoveAllSpaceAndConcertToPascalCase(string status)
        {
            var textInfo = new System.Globalization.CultureInfo("en-US").TextInfo;
            var titleCaseStr = textInfo.ToTitleCase(status);
            string result = titleCaseStr.Replace("_","").Replace(" ", "");

            return result;
        }

for those who needs a non regex solution

 public static string RemoveAllSpaceAndConcertToPascalCase(string status)
        {
            var textInfo = new System.Globalization.CultureInfo("en-US").TextInfo;
            var titleCaseStr = textInfo.ToTitleCase(status);
            string result = titleCaseStr.Replace("_","").Replace(" ", "");

            return result;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文