1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- def to_camel_case(s):
-
- return s[0] + s.title().translate({ord('_'):None, ord('-'):None})[1:] if s else s
- '''
- # replace - замена
- # Интересный for по списку и join
- def to_camel_case(text):
- removed = text.replace('-', ' ').replace('_', ' ').split()
- if len(removed) == 0:
- return ''
- return removed[0]+ ''.join([x.capitalize() for x in removed[1:]])
- '''
- '''
- # Как и в первой функции только без защиты от пустой строки
- def to_camel_case(text):
- return text[:1] + text.title()[1:].replace('_', '').replace('-', '')
- '''
- '''
- # Разобраться с регулярками
- import re
- def to_camel_case(text):
- return re.sub('[_-](.)', lambda x: x.group(1).upper(), text)
- '''
- '''
- # Микс из первого и второго варианта
- def to_camel_case(text):
- words = text.replace('_', '-').split('-')
- return words[0] + ''.join([x.title() for x in words[1:]])
- '''
- '''
- # Перебор по символу
- def to_camel_case(text):
- cap = False
- newText = ''
- for t in text:
- if t == '_' or t == '-':
- cap = True
- continue
- else:
- if cap == True:
- t = t.upper()
- newText = newText + t
- cap = False
- return newText
- '''
- '''
- # Мозгодробительный вариант
- def to_camel_case(text):
- return "".join([i if n==0 else i.capitalize() for n,i in enumerate(text.replace("-","_").split("_"))])
- '''
- print(to_camel_case("the-stealth-warrior"))
- print(to_camel_case("The_Stealth_Warrior"))
|