One of the things that is often used in the JavaScript Prototype library is the camelize function which camel-cases the specified string. Unfortunately, just as this function doesn’t exist natively in JavaScript, it also doesn’t in VBScript. If you do need an equivalent function to that which is available in the Prototype library, you can use the following function definition:


Public Function CamelCase(str)
  Dim arr, i
  arr = Split(str, "-")
  For i = LBound(arr) + 1 To UBound(arr)
    arr(i) = UCase(Left(arr(i), 1)) & Mid(arr(i), 2)
  Next
  CamelCase = Join(arr, "")
End Function

To use this function, simply pass the string that you want to be camel-cased, and the modified string will be returned:


MsgBox CamelCase("border-color-left")
Categories: BlogVBAVBScript

2 Comments

Fabio Zendhi Nagao · October 9, 2012 at 9:36 AM

Your CamelCase function is flawed. For example:

dim id
id = CamelCase(“BoRDeR-CoLoR-LeFT”)’

Outputs: BoRDeRCoLoRLeFT, which isn’t in the CamelCase format (borderColorLeft expected). Maybe changing from Split(str, “-“) to Split(LCase(str), “-“) fix the problem…

    Chris West · October 9, 2012 at 12:43 PM

    The reason I did this is because I figure that if other letters not following the dash are capitalized, they must be in that form on purpose. Another important thing to note is that other major libraries such as PrototypeJS implement this functionality without lowercasing all of the other letters. The following is an example that you can run with PrototypeJS:
    [code language=javascript]
    alert("cool-stuFf".camelize()); // coolStuFf
    [/code]

    With these facts in mind, I will stand behind my original definition of CamelCase. On the other hand, the reason I put code on my website is so that others can use the code and modify it to suit their needs.

Leave a Reply to Chris West Cancel reply

Your email address will not be published. Required fields are marked *