在驼峰式标记的单词之间插入空格

本文关键字:单词 之间 插入 空格 | 更新日期: 2023-09-27 17:53:41

是否有一个很好的函数来转动像

这样的东西

FirstName

:

名字吗?

在驼峰式标记的单词之间插入空格

参见:.NET -如何分割"caps"分隔字符串到数组?

尤其是

:

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

这是我在这类事情上经常使用的一个扩展方法

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: "Uppercase, Uppercase, Lowercase"(这将匹配IBMMake中的MMa并得到IBM Make),以及2。"小写,大写"(这将匹配MakeStuff中的eS)。这涵盖了所有驼峰断点。
提示:用连字符替换空格并调用ToLower来生成HTML5数据属性名。

最简单的方法:

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

您可以使用正则表达式:

Match    ([^^])([A-Z])
Replace  $1 $2
在代码:

String output = System.Text.RegularExpressions.Regex.Replace(
                  input,
                  "([^^])([A-Z])",
                  "$1 $2"
                );
    /// <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

Regex:

http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspxhttp://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到Title Case,使用MatchEvaluator:公共字符串toTitleCase(匹配m) {charc = m.Captures [0] value [0];返回((c> = a),和(c<= ' z ')) ? Char.ToUpper (c) .ToString():"+ c;}并稍微更改一下正则表达式用这句话:正则表达式。("UpperCamelCase或替换lowerCamelCase"@"(' [a - z] | ' b [a - z])",新的

MatchEvaluator (toTitleCase));