Added more functions to string
This commit is contained in:
parent
0fec5719bc
commit
036b9dcc2d
@ -1,4 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
|
import string
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
class String:
|
class String:
|
||||||
@ -7,35 +9,58 @@ class String:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def convert_to_snake_case(name: str) -> str:
|
def convert_to_camel_case(chars: str) -> str:
|
||||||
|
"""
|
||||||
|
Converts string to camel case
|
||||||
|
:param chars:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
converted_name = chars
|
||||||
|
char_set = string.punctuation + ' '
|
||||||
|
for char in char_set:
|
||||||
|
if char in converted_name:
|
||||||
|
converted_name = ''.join(word.title() for word in converted_name.split(char))
|
||||||
|
|
||||||
|
return converted_name
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def convert_to_snake_case(chars: str) -> str:
|
||||||
"""
|
"""
|
||||||
Converts string to snake case
|
Converts string to snake case
|
||||||
:param name:
|
:param chars:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
# convert to train-case to CamelCase
|
# convert to train-case to CamelCase
|
||||||
if '-' in name:
|
if '-' in chars:
|
||||||
name = ''.join(word.title() for word in name.split('-'))
|
chars = ''.join(word.title() for word in chars.split('-'))
|
||||||
|
|
||||||
pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
|
pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
|
||||||
pattern2 = re.compile(r'([a-z0-9])([A-Z])')
|
pattern2 = re.compile(r'([a-z0-9])([A-Z])')
|
||||||
file_name = re.sub(pattern1, r'\1_\2', name)
|
file_name = re.sub(pattern1, r'\1_\2', chars)
|
||||||
return re.sub(pattern2, r'\1_\2', file_name).lower()
|
return re.sub(pattern2, r'\1_\2', file_name).lower()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def first_to_upper(string: str) -> str:
|
def first_to_upper(chars: str) -> str:
|
||||||
"""
|
"""
|
||||||
Converts first char to upper
|
Converts first char to upper
|
||||||
:param string:
|
:param chars:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return f'{string[0].upper()}{string[1:]}'
|
return f'{chars[0].upper()}{chars[1:]}'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def first_to_lower(string: str) -> str:
|
def first_to_lower(chars: str) -> str:
|
||||||
"""
|
"""
|
||||||
Converts first char to lower
|
Converts first char to lower
|
||||||
:param string:
|
:param chars:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
return f'{string[0].lower()}{string[1:]}'
|
return f'{chars[0].lower()}{chars[1:]}'
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def random_string(chars: str, length: int) -> str:
|
||||||
|
"""
|
||||||
|
Creates random string by given chars and length
|
||||||
|
"""
|
||||||
|
|
||||||
|
return ''.join(random.choice(chars) for _ in range(length))
|
||||||
|
Loading…
Reference in New Issue
Block a user