在骆驼形标记上插入单词之间的空格

有没有一个很好的function来打开类似的东西

名字

对此:

名字?

请参阅: .NET – 如何将“大写”分隔string拆分为数组?

特别:

Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[AZ])", " $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”之类的string,将其转换为“IBM Make Stuff And Sell It”(IIRC)

你可以使用正则expression式:

 Match ([^^])([AZ]) Replace $1 $2 

在代码中:

 String output = System.Text.RegularExpressions.Regex.Replace( input, "([^^])([AZ])", "$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 

最简单的方法:

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

正则expression式:

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转换为Title Case,使用下面这一行:Regex.Replace(“UpperCamelCase”,@“(\ B [AZ])”,@“$ 1”);

要从lowerCamelCase和UpperCamelCase转换为Title Case,使用MatchEvaluator:public string toTitleCase(Match m){char c = m.Captures [0] .Value [0]; return((c> ='a')&&(c <='z'))?Char.ToUpper(c).ToString():“”+ c; Regex.Replace(“UpperCamelCase or lowerCamelCase”,@“(\ b [az] | \ B [AZ])”,新的MatchEvaluator(toTitleCase));