【问题标题】:Insert spaces between words on a camel-cased token [duplicate]在驼峰式标记上的单词之间插入空格[重复]
【发布时间】:2011-04-26 20:59:57
【问题描述】:

是否有一个很好的功能来转动类似的东西

名字

到这里:

名字?

【问题讨论】:

  • 这对我有用:Regex.Replace(s, "([A-Z0-9]+)", " $1").Trim()。如果您想拆分每个大写字母,只需删除加号即可。

标签: c#


【解决方案1】:

见:.NET - How can you split a "caps" delimited string into an array?

特别是:

Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")

【讨论】:

  • 稍微好一点的正则表达式可以正确处理首字母缩略词:@"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))"
  • @AviadP。首字母缩略词失败,例如"HTMLGuide""H TML Guide"
  • 修改为 Regex.Replace("ThisIsMy1stCapsDelimitedString", "(\\B[A-Z0-9])", " $1") 也可以拆分数字。
  • 这个确实有效:(?codeproject.com/Articles/108996/…
【解决方案2】:

这是我广泛用于此类事情的扩展方法

public static string SplitCamelCase( this string str )
{
    return Regex.Replace( 
        Regex.Replace( 
            str, 
            @"(\P{Ll})(\P{Ll}\p{Ll})", 
            "$1 $2" 
        ), 
        @"(\p{Ll})(\P{Ll})", 
        "$1 $2" 
    );
}

它还处理像IBMMakeStuffAndSellIt 这样的字符串,将其转换为IBM Make Stuff And Sell It (IIRC)。

语法解释(credit):

{Ll} 是 Unicode 字符类别“字母小写”(与 {Lu}“字母大写”相对)。 P 是负匹配,而p 是正匹配,所以\P{Ll} 字面意思是“非小写”,p{Ll} 是“小写”。
所以这个正则表达式分成两种模式。 1:“大写、大写、小写”(将匹配 IBMMake 中的 MMa 并导致 IBM Make),以及 2.“小写,大写”(将匹配 MakeStuff 中的 eS )。这涵盖了所有驼峰式断点。
提示:用连字符替换空格并调用 ToLower 以生成 HTML5 数据属性名称。

【讨论】:

  • 谢谢,效果很好!我只想补充一点,我相信如果使用 Compiled 选项静态创建 Regex 对象而不是使用静态 Regex.Replace 方法会更快。
  • 语法解释:{Ll} 是 Unicode 字符类别“字母小写”(相对于 {Lu}“字母大写”)。 'P' 是否定匹配,而 'p' 是肯定匹配,所以\P{Ll} 字面意思是“非小写”,p{Ll} 是“小写”。所以这个正则表达式分成两种模式。 1:“大写、大写、小写”(将匹配“IBMMake”中的“MMa”并生成“IBM Make”),以及 2.“小写、大写”(匹配“MakeStuff”中的“eS” ')。这涵盖了所有骆驼案例断点。提示:用连字符替换空格并调用ToLower 以生成 html5 数据属性名称。
  • 我比公认的答案更喜欢这个,因为它不会创建“I B M Make Stuff And Sell It”
  • \p{Lu}代替\P{Ll}有什么问题吗?
  • @Andrew 我认为通常的约定是将前导首字母缩写词的所有字母小写,因此它将是“ibmMakeStuffAndSellIt”。
【解决方案3】:

最简单的方法:

var res = Regex.Replace("FirstName", "([A-Z])", " $1").Trim();

【讨论】:

    【解决方案4】:

    你可以使用正则表达式:

    Match    ([^^])([A-Z])
    Replace  $1 $2
    

    在代码中:

    String output = System.Text.RegularExpressions.Regex.Replace(
                      input,
                      "([^^])([A-Z])",
                      "$1 $2"
                    );
    

    【讨论】:

    • 这会从“USA”创建“U_S_A_”(空格替换为下划线)吗?也就是说,它会附加一个尾随空格吗?
    • @Jim Mischel:不,它只是在大写字母前面。
    • “USA”改成“U SA”,“ASCII”改成“AS CI I”。
    【解决方案5】:
        /// <summary>
        /// Parse the input string by placing a space between character case changes in the string
        /// </summary>
        /// <param name="strInput">The string to parse</param>
        /// <returns>The altered string</returns>
        public static string ParseByCase(string strInput)
        {
            // The altered string (with spaces between the case changes)
            string strOutput = "";
    
            // The index of the current character in the input string
            int intCurrentCharPos = 0;
    
            // The index of the last character in the input string
            int intLastCharPos = strInput.Length - 1;
    
            // for every character in the input string
            for (intCurrentCharPos = 0; intCurrentCharPos <= intLastCharPos; intCurrentCharPos++)
            {
                // Get the current character from the input string
                char chrCurrentInputChar = strInput[intCurrentCharPos];
    
                // At first, set previous character to the current character in the input string
                char chrPreviousInputChar = chrCurrentInputChar;
    
                // If this is not the first character in the input string
                if (intCurrentCharPos > 0)
                {
                    // Get the previous character from the input string
                    chrPreviousInputChar = strInput[intCurrentCharPos - 1];
    
                } // end if
    
                // Put a space before each upper case character if the previous character is lower case
                if (char.IsUpper(chrCurrentInputChar) == true && char.IsLower(chrPreviousInputChar) == true)
                {   
                    // Add a space to the output string
                    strOutput += " ";
    
                } // end if
    
                // Add the character from the input string to the output string
                strOutput += chrCurrentInputChar;
    
            } // next
    
            // Return the altered string
            return strOutput;
    
        } // end method
    

    【讨论】:

    • 经过测试,我意识到这是最快的方法。谢谢
    【解决方案6】:

    正则表达式:

    http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx http://stackoverflow.com/questions/773303/splitting-camelcase

    (可能是最好的 - 见第二个答案) http://bytes.com/topic/c-sharp/answers/277768-regex-convert-camelcase-into-title-case

    从 UpperCamelCase 转换为 标题案例,使用这一行: Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");

    从 lowerCamelCase 转换 和 UpperCamelCase 到标题大小写,使用 MatchEvaluator:公共字符串 toTitleCase(匹配 m){ 字符 c=m.Captures[0].Value[0];返回 ((c>='a')&&(c

    【讨论】:

      猜你喜欢
      • 2022-11-02
      • 2011-03-25
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      相关资源
      最近更新 更多