-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_string.py
47 lines (40 loc) · 1.13 KB
/
u_string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding:utf-8 -*-
# @Author: wzy
# @Time: 2021/3/18
# 字符串和字符相关的工具函数
__all__ = ['contains_chinese', 'hump_2_underline', 'underline_2_hump']
def contains_chinese(text):
"""
检查整个字符串是否包含中文
:param text: 需要检查的字符串
:return: bool
"""
for ch in text:
if u'\u4e00' <= ch <= u'\u9fff':
return True
return False
def hump_2_underline(text):
"""
大小驼峰转换为下划线
:param text: 需要转换的字符串
:return:
"""
res = []
for index, char in enumerate(text):
if char.isupper() and index != 0:
res.append("_")
res.append(char)
return ''.join(res).lower()
def underline_2_hump(text, big=True):
"""
下划线连接转为大小驼峰
:param text: 需要转换的字符串
:param big: 是否为大驼峰, 默认为True, 为False时表示要转换小驼峰
"""
arr = text.lower().split('_')
res = []
for i in arr:
res.append(i[0].upper() + i[1:])
if not big:
res[0] = res[0][0].lower() + res[0][1:]
return ''.join(res)