在Python中将字符串转换为驼峰camelCase
在这里,我们正在实现一个python程序,将给定的字符串转换为camelCase。
camelCase的示例:
String: "Hello world"
camelCase string: "helloWorld"程序:
# 导入模块
from re import sub
# 函数将字符串转换为camelCase
def camelCase(string):
string = sub(r"(_|-)+", " ", string).title().replace(" ", "")
return string[0].lower() + string[1:]
# 主要代码
s1 = "Hello world"
s2 = "Hello,world"
s3 = "Hello_world"
s4 = "hello_world.txt_nhooo-WEBSITE"
print("s1: ", s1)
print("camelCase(s1): ", camelCase(s1))print()print("s2: ", s2)
print("camelCase(s2): ", camelCase(s2))print()print("s3: ", s3)
print("camelCase(s3): ", camelCase(s3))print()print("s4: ", s4)
print("camelCase(s4): ", camelCase(s4))print()输出结果
s1: Hello world camelCase(s1): helloWorld s2: Hello,world camelCase(s2): hello,World s3: Hello_world camelCase(s3): helloWorld s4: hello_world.txt_nhooo-WEBSITE camelCase(s4): helloWorld.TxtnhoooWebsite
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短