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'))
  • 相关阅读:
    线段树
    数据结构<三> 队列
    数据结构<二>双向链表
    数据结构<一>单链表
    扩展欧几里德算法
    90 个 node.js 扩展模块,我们疯了
    nodejs的查询构造器
    express的路由配置优化
    express路由方案
    Redis学习笔记~目录
  • 原文地址:https://www.cnblogs.com/pipile/p/12604977.html
Copyright © 2011-2022 走看看