{"id":256,"date":"2010-09-17T01:04:11","date_gmt":"2010-09-16T15:04:11","guid":{"rendered":"http:\/\/jcrawfor74.wordpress.com\/?p=256"},"modified":"2013-01-24T01:36:02","modified_gmt":"2013-01-23T14:36:02","slug":"stringutil-class-space-before-a-capital-letter","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2010\/09\/17\/stringutil-class-space-before-a-capital-letter\/","title":{"rendered":"StringUtil class &#8211; Space Before a Capital Letter"},"content":{"rendered":"<div id=\"ntsbl-2438749393\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><p>Hi,<\/p>\n<p>I often use enumerations in code that I want to display as text on the screen. For example the state of an object recorded in an enum like<br \/>\n[csharp]<br \/>\npublic enum StageType<br \/>\n{<br \/>\n\tPreRelease = 0,<br \/>\n\tProposed = 1,<br \/>\n\tOpen = 2,<br \/>\n\tClosed = 3<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>So if I get the matching value out of the database and I want to render a textual representation of the enum to the UI I might do:<\/p>\n<p>lblStatus.Text = ((StageType) iStage).ToString(); \/\/ where iStage is an int value representing the enum.<\/p>\n<p>So this is great apart from the first item  PreRelease will be shown without a space.<\/p>\n<p>Step in my amazing class which will parse the string and add a space before a capital so &#8220;PreRelease&#8221; becomes &#8220;Pre Release&#8221;<br \/>\nThis way I can use any Camel case enum string and have it render to the UI nicely.<\/p>\n<p>[csharp]<br \/>\npublic static class StringUtil<br \/>\n{<\/p>\n<p>\t\/\/\/<br \/>\n\t\/\/\/ Returns a string with a space after a capital<br \/>\n\t\/\/\/<br \/>\n\t\/\/\/<br \/>\n\t\/\/\/<br \/>\n\tpublic static string SpaceOnCapital(string word)<br \/>\n\t{<br \/>\n\t\tStringBuilder sb = new StringBuilder();<\/p>\n<p>\t\tforeach (char c in word)<br \/>\n\t\t{<br \/>\n\t\t\tif (sb.Length == 0)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tsb.Append(c);<br \/>\n\t\t\t}<br \/>\n\t\t\telse<br \/>\n\t\t\t{<br \/>\n\t\t\t\t\/\/ not the first character<br \/>\n\t\t\t\t\/\/ ch<br \/>\n\t\t\t\tif (IsCapital(c))<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\tsb.AppendFormat(&quot;{0}&quot;, c);<br \/>\n\t\t\t\t}<br \/>\n\t\t\t\telse<br \/>\n\t\t\t\t{<br \/>\n\t\t\t\tsb.Append(c);<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\treturn sb.ToString();<br \/>\n\t}<\/p>\n<p>\tpublic static bool IsCapital(char c)<br \/>\n\t{<br \/>\n\t\tint ascii = (int)c;<br \/>\n\t\treturn (ascii &gt;= 65 &amp;&amp; ascii &lt;= 90);<br \/>\n\t}<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, I often use enumerations in code that I want to display as text on the screen. For example the state of an object recorded [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}