将包含_或-的字符串最开始的字母小写,其余的第一个字母大写

Posted 哈商大-瀚阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将包含_或-的字符串最开始的字母小写,其余的第一个字母大写相关的知识,希望对你有一定的参考价值。

from re import sub

def camel(s):
    print(s)
    # some_database_field_name
    # Some label that needs to be camelized
    # some-javascript-property
    # some-mixed_string with spaces_underscores-and-hyphens

    print(sub(r"(_|-)+", " ", s))
    # some database field name
    # Some label that needs to be camelized
    # some javascript property
    # some mixed string with spaces underscores and hyphens

    print((sub(r"(_|-)+", " ", s)).title())
    # Some Database Field Name
    # Some Label That Needs To Be Camelized
    # Some Javascript Property
    # Some Mixed String With Spaces Underscores And Hyphens

    print((sub(r"(_|-)+", " ", s)).title().replace(" ", ""))
    # SomeDatabaseFieldName
    # SomeLabelThatNeedsToBeCamelized
    # SomeJavascriptProperty
    # SomeMixedStringWithSpacesUnderscoresAndHyphens

    s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
    print(s)
    # SomeDatabaseFieldName
    # SomeLabelThatNeedsToBeCamelized
    # SomeJavascriptProperty
    # SomeMixedStringWithSpacesUnderscoresAndHyphens

    print(s[0].lower())
    # s
    # s
    # s
    # s
    print(s[0].lower() + s[1:])
    # someDatabaseFieldName
    # someLabelThatNeedsToBeCamelized
    # someJavascriptProperty
    # someMixedStringWithSpacesUnderscoresAndHyphens

    # s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
    # print(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

 

 

 

 


2020-05-03

以上是关于将包含_或-的字符串最开始的字母小写,其余的第一个字母大写的主要内容,如果未能解决你的问题,请参考以下文章

字符操作

Python 改变字符串大小写的方法

Python 改变字符串大小写的方法

CF Educational Round 51 题解

1042: 元音字母转换

java语言,取字符串第一个字母最简单的方法是啥?