zoukankan      html  css  js  c++  java
  • 破折号/下划线分隔的单词转换为驼峰式大小写

    #方法1
    import re
    def to_camel_case(text):
        # your code here
        rlt = ''
        for i, value in enumerate(re.split('_|-',text)):
            if i != 0:
                value = value[0].upper() + value[1:]
            rlt += value
        return rlt
    
    print(to_camel_case('The-Stealth-Warrior'))
    
    #方法2
    def to_camel_case2(text):
        '''
        capitalize()与title()都可以实现字符串首字母大写.
        主要区别在于:
        capitalize(): 字符串第一个字母大写
        title(): 字符串内的所有单词的首字母大写
    
        例如:
    
        >>> str='huang bi quan'
        >>> str.capitalize()
        'Huang bi quan'          #第一个字母大写
        >>> str.title()
        'Huang Bi Quan'          #所有单词的首字母大写
        非字母开头的情况:
    
        >>> str='深圳luohu'
        >>> str.capitalize()
        '深圳luohu'               #输出内容不变
        >>> str.title()
        '深圳Luohu'               #第一个字母大写
        '''
        # your code here
        return ''.join(i.title() for i in re.split("-|_",text))
    
    print(to_camel_case2('the-stealth-warrior'))
  • 相关阅读:
    Node.js:事件循环
    Node.js:回调函数
    Node.js:REPL(交互式解释器)
    Node.js:NPM 使用介绍
    Node.js:创建第一个应用
    Node.js:安装配置
    Node.js:教程
    Node.js:目录
    Node.js:template
    虚拟化之xenserver
  • 原文地址:https://www.cnblogs.com/pipile/p/12604977.html
Copyright © 2011-2022 走看看