代码之家  ›  专栏  ›  技术社区  ›  Paul Fryer

函数使pascal成为case?(C)

  •  14
  • Paul Fryer  · 技术社区  · 15 年前

    我需要一个函数,它需要一个字符串和“pascal case”。新单词开始的唯一指示符是下划线。下面是一些需要清理的字符串示例:

    1. price_old=>应该是priceld
    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);
    }
    

    上面的函数不做的事情是删除下划线,并在下划线右侧“插入”字符。

    此外,关于如何将没有任何指示器(如下划线)的字符串作为pascal大小写的任何想法。例如:

    1. 公司来源
    2. 金融趋势
    3. 会计变更类型

    这里的主要挑战是确定一个词的结尾和另一个词的开头。我想我需要一些查找字典来确定新词从哪里开始?我们那里有图书馆可以做这种事吗?

    谢谢,

    保罗

    6 回复  |  直到 8 年前
        1
  •  23
  •   theburningmonk    15 年前

    你可以使用 TextInfo.ToTitleCase 方法,然后删除“u”字符。

    所以,使用扩展方法,我有:

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

    你可以这样做:

    var s = "price_old";
    s.ToTitleCase().Replace("_", string.Empty);
    
        2
  •  11
  •   Jan Jongboom    15 年前

    首先很简单:

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

    收益率 PriceOld

    第二件事是更困难。AS companysource 可以是 CompanySource 或者也许 CompanysOurce 可以自动化,但有很大的缺陷。你需要一个 English dictionary 做一些猜测(啊,我是说 阿洛特 )在哪个词的组合是正确的。

        3
  •  4
  •   Rubens Farias    15 年前

    试试这个:

    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
    
        4
  •  2
  •   Alan Moore Chris Ballance    15 年前

    带下划线:

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

    不带下划线:

    你自己在那里。但是继续搜索,如果 没有人 以前做过。

        5
  •  0
  •   Frank Bollack    15 年前

    对于第二个拆分连接词的问题,您可以使用我们最好的朋友Google&Co。如果您的连接输入是由常见的英语单词组成的,则搜索引擎将单个单词的命中率作为备选搜索查询。

    如果输入示例输入,谷歌和必应建议如下:

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

    this exaple .

    写一个小屏幕刮刀应该相当容易。

        6
  •  0
  •   Iman    8 年前

    对于那些需要非regex解决方案的人

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