go 语言编程——Convert string to camel case

该博客讨论了如何在Go(Golang)中将由短划线或下划线分隔的单词转换为驼峰式命名。内容涵盖题目描述、样例说明、解题思路以及代码实现,强调了字符串切割、大小写转换和数组遍历等关键知识点。
摘要由CSDN通过智能技术生成

题目描述

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

样例说明

"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

题目分析

结合题目描述以及样例:将给出的字符串转换成标准的驼峰式命名。

输入字符:

  1. 字符串中含有"-"
  2. 字符串中含有"_"
  3. 以上两种情况都没有

返回字符:"-“或者”_"标识的字符串中,第一个不发生改变,之后每一个中间的字符的首字母均转换成大写,且标识消失。

思路:①字符串依据"-“或者”_"切割;②根据切割之后的字符修改除第一个字符之外的字符串;③返回拼接之后的字符串

代码

func ToCamelCase(s string) string{
   
        var strArr []string
        if strings.Contains(s,"-") {
   
                strArr=strings.Split(s,"-")
        }else if strings.Contains(s,"_") 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值