Quickly... yes its got an American z in it, but here is some code to convert a string into Camel case, upper or lower.
/// <summary>
/// Convert the passed type to camel case
///
/// Capitalize the first letter of each word and remove the spaces
/// </summary>
/// <param name="value">Value to convert to camel case</param>
/// <param name="upper">Upper case the first letter</param>
/// <returns>Camelized type</returns>
public static string Camelize(this string value, bool upper) {
if (string.IsNullOrEmpty(value)) return value;
var output = new StringBuilder();
var length = 0;
for (var index = 0; index < value.Length; index++) {
var c = value[index];
if (c >= 'a' && c <= 'z'){
if (length==0 && (upper || output.Length > 0))
c = (char)(c - 32);
length++;
} else if(c >= 'A' && c <= 'Z'){
if (length==0 && (!upper && output.Length == 0))
c = (char)(c + 32);
length++;
} else if ((c >= '0' && c <= '9') || c == '_') {
length++;
} else length = 0;
if (length>0) output.Append(c);
}
return output.ToString();
}
/// <summary>
/// Convert the passed type to camel format
///
/// Capitalize the first letter of each word and remove the spaces
/// </summary>
/// <param name="type">Value to convert to lower camel case</param>
/// <returns>Camelized type</returns>
public static string Camelize(this string value) {
return Camelize(value, false);
}
The value is split by "word break" as defined in Regular Expressions (\b), each word is capitalised, the first word is capitalised if upper is true. No other letter in the word is changed, if it were set to lower case then calling the function twice on the same value would give two different results - which I didn't want.
By the way, having the American spelling is just to keep it in-line with .net, being American, I think it just makes all kinds of sense.
A test or two
Assert.AreEqual("oneTwoThree", "One two three".Camelize());
Assert.AreEqual("OneTwoThree", "One two three".Camelize(true));
Assert.AreEqual("oneTwoThree", "One-two.three".Camelize());
Assert.AreEqual("oneTwoThree", "One two$three".Camelize());
Assert.AreEqual("oneTwo2Three3", "One two2 three3".Camelize());
Assert.AreEqual("one_twoThree", "One_two three".Camelize());
| < | February 2012 | |||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 29 | 30 | 31 | 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 1 | 2 | 3 |
Add-ins AJAX ASP.NET MVC Browsers C# Caching Compression CORS CSS CV Data Database DependencyResolver Development Entity Framework Error Handling File Upload Forms GDI+ HTML HTML Editor HTTP Interfaces JavaScript JQuery MCE MetadataProvider MSBuild Numbers Objects Patterns Progressive Enhancement Projects Publish Regex Resources Security SEO SMTP Source Control Strings Sub-Collections TDD Tools Twitter User Interface WCF Web Development WHS WMC XLinq XML
11 hours ago
verge
Microsoft teases Windows 8 'Consumer Preview' with Bing betta fish site http://t.co/lcJICazH