是否有一个很好的功能来转变
姓
到此:
名字?
答案 0 :(得分:154)
请参阅:.NET - How can you split a "caps" delimited string into an array?
特别是:
Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")
答案 1 :(得分:107)
这是我广泛用于此类事情的扩展方法
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)
答案 2 :(得分:7)
您可以使用正则表达式:
Match ([^^])([A-Z])
Replace $1 $2
在代码中:
String output = System.Text.RegularExpressions.Regex.Replace(
input,
"([^^])([A-Z])",
"$1 $2"
);
答案 3 :(得分:6)
最简单的方法:
var res = Regex.Replace("FirstName", "([A-Z])", " $1").Trim();
答案 4 :(得分:3)
/// <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
答案 5 :(得分:1)
正则表达式:
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到Title Case,使用 MatchEvaluator:公共字符串 toTitleCase(Match m){char C = m.Captures [0]。价值[0];返回 ((c取代; = 'A')及及(℃下= 'Z'))Char.ToUpper(c)中的ToString():”? “+ c;}并改变你的正则表达式 用这一行: Regex.Replace(“UpperCamelCase或 lowerCamelCase”,@ “(\ B [A-Z] | \ B [A-Z])”,新 MatchEvaluator(toTitleCase));