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());
| < | May 2012 | |||||
|---|---|---|---|---|---|---|
| S | M | T | W | T | F | S |
| 29 | 30 | 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 | 30 | 31 | 1 | 2 |
Add-ins AJAX ASP.NET MVC Browsers C# Caching CodeDom Compression CORS CSS CV Data Database DependencyResolver Development Dynamic Entity Framework Error Handling Extend 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 Templates Tools Twitter User Interface WCF Web Development WHS WMC XLinq XML
1 hours ago
TheNextWeb
Bing's search API now live on the Windows Azure Marketplace http://t.co/utX8uOuG by @alex
15/05/2012
WindowsAzure
Announcing the MEET Windows Azure Event! Streamed online June 7th. Register at http://t.co/bObzTAuL #MEETAzure #WindowsAzure
59 minutes ago
commadelimited
Buy the @amazon Kindle version of mine and @cfjedimaster's @jquerymobile book for $10 today: http://t.co/PWRZ2dkd
just now
MSCloud
RT @msPartner: Bob Kelly offers details on Microsoft’s #cloud strategy: http://t.co/GECtwycA via @rcpmag #Azure #mspartner ^NW
just now
Adobe_News1
Learn about the PhoneGap Connection API http://t.co/ztIb9Vab