Human readable to camel case in Scala
After searching for a simple way to convert a string like I am a nice string (human friendly) into something like IAmANiceString (camel-case) and finding formulas only for the opposite, I decided to write the solution myself.
I’m sure it’s not the fastest and it doesn’t use regular expressions, but it’s simple to read (for me at least) and should allow me to build such string transformations myself in the future.
Anyways, here the function:
def humanToCamel(original: String): String =
original
.foldLeft(("", true)) {
case ((acc, upper), e) => (if (e == ' ') acc else if (upper) acc + e.toUpper else acc + e, e == ' ')
}
._1
Hope it’s useful for you, and if you have any feedback, feel free to leave it on the comments.

0 comentarios
Se conservan los comentarios originales. No se admiten nuevos comentarios.