.net - ToPascalCase() C# for all caps Abbreviations -
I am using ReSharper and am trying to follow its default rules.
In the part of my code I need to convert Pascal Cas to string.
I have tried many ways but works for all those who incorporate all capital indices.
EX:
MPSUser -> Still should be MPSUser (MpsUser should be) ArticleID - & gt; Still, Article ID (should be the article) closeMethod - & gt; Can anyone help me in preparing a method to turn Pascal Cas to any string? CloseMethod
Thanks!
The only built-in method is I PascalCase
, and all caps by this design word Does not handle, to work around it, I have given a custom regular expression that can detect all the word parts, and then they are converted into a title / Pascal case individually:
string ToPascalCase (string s) {/ / Find part of the word using the following rules: // 1. Start All lowercase word / 2 from starting 2. ALL CAPS is a word. // 3. The first letter cap, after all lowercase is a word / 4. According to the entire string 1,2,3, words should rot. // Note that together with 2 and 3, make sure the MPSUSE is parsed as "MPS" + "User". Var m = Regex.Match (s, "^ (? & Lt; terms & gt; ^ [az] + | [az] + | [az] [a-z] +)" $ "); Var G = M. Group ["word"]; // Take each word and convert it individually to title seas // to generate the final note. ToLower / toTitleCase note before / use because all the cap Is considered as the abbreviation var t = thread. Current. Var sb = new stringbilder (); foreign (vcg capture. Cast and left; capture & gt; ()) sb.Append (t. ToTitleCase (c.value.ToLower ()); return sb.ToString ();}
This function should handle normal usage cases:
s | ToPascalCase MPSUser | MPSUser Article | Article ID IID Lock Mode | CloseMethod
Comments
Post a Comment