Files
30-seconds-of-code/snippets/camel.md
Angelos Chalaris 4d544ad192 Update camel.md
2019-08-23 10:13:28 +03:00

675 B

title, tags
title tags
camel string,regexp,intermediate

Converts a string to camelcase.

Break the string into words and combine them capitalizing the first letter of each word, using a regexp, title() and lower.

import re

def camel(s):
  s = re.sub(r"(\s|_|-)+", " ", s).title().replace(" ", "")
  return s[0].lower() + s[1:]
camel('some_database_field_name'); # 'someDatabaseFieldName'
camel('Some label that needs to be camelized'); # 'someLabelThatNeedsToBeCamelized'
camel('some-javascript-property'); # 'someJavascriptProperty'
camel('some-mixed_string with spaces_underscores-and-hyphens'); # 'someMixedStringWithSpacesUnderscoresAndHyphens'