Ant
1 note

Camelize

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.

Tags Strings

Ant

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());


Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

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