How to replace symbol AND make next letter uppercase in Go

huangapple go评论96阅读模式
英文:

How to replace symbol AND make next letter uppercase in Go

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go的初学者实习生。
我无法弄清楚如何不仅替换一个符号,而且还要将下一个字母变成大写。

任务:
完成方法/函数,将破折号/下划线分隔的单词转换为驼峰命名法。输出中的第一个单词只有在原始单词首字母大写的情况下才大写(称为大驼峰命名法,也经常称为帕斯卡命名法)。

我尝试使用正则表达式方法实现:

re, _ := regexp.Compile(`/[-_]\w/ig`)
res := re.FindAllStringSubmatch(s, -1)
return res

但是我无法返回res,因为它是一个切片/数组,而我需要返回一个字符串。

我的代码:

package main

import (
	"fmt"
	"strings"
)

func ToCamelCase(s string) string {
	s = strings.ReplaceAll(s, "-", "")
	s = strings.ReplaceAll(s, "_", "")
	return s
}

func main() {
	var s string
	fmt.Scan(&s)
	fmt.Println(ToCamelCase(s))
}

输入:
"the-stealth-warrior" 或 "the_stealth_warrior"

输出:
"theStealthWarrior" 或 "TheStealthWarrior"

我的输出:thestealthwarrior

英文:

I'm beginner trainee in Go.
I can't figure out how not just replace a symbol, but to make next letter Uppercase in Go.

Task:
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).

I tried to implement regexp methods with:

re, _ := regexp.Compile(`/[-_]\w/ig`)
	res := re.FindAllStringSubmatch(s, -1)
return res

But i can't return res because it's slice/array, but i need to return just string.

My code:

package main

import (
	"fmt"
	"strings"
)

func ToCamelCase(s string) string {
	s = strings.ReplaceAll(s, "-", "")
	s = strings.ReplaceAll(s, "_", "")
	return s
}

func main() {
	var s string
	fmt.Scan(&s)
	fmt.Println(ToCamelCase(s))
}

Input:
"the-stealth-warrior" or "the_stealth_warrior"

Output:
"theStealthWarrior" or "TheStealthWarrior"

My Output: thestealthwarrior

答案1

得分: 2

你需要在Go字符串字面值中定义不带正则表达式定界符的正则表达式,并且使用ReplaceAllStringFunc函数更方便:

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func ToCamelCase(s string) string {
	re, _ := regexp.Compile(`[-_]\w`)
	res := re.ReplaceAllStringFunc(s, func(m string) string {
		return strings.ToUpper(m[1:])
	})
	return res
}

func main() {
	s := "the-stealth-warrior"
	fmt.Println(ToCamelCase(s))
}

请参见Go playground

输出结果为theStealthWarrior

[-_]\w模式匹配-_,然后是任意单词字符。如果你想排除_,可以使用[^\W_]代替\w

英文:

You need to define the regex without regex delimiters in Go string literals, and it is more convenient to use the ReplaceAllStringFunc function:

package main

import (
	"fmt"
	"regexp"
	"strings"
)

func ToCamelCase(s string) string {
	re, _ := regexp.Compile(`[-_]\w`)
	res := re.ReplaceAllStringFunc(s, func(m string) string {
		return strings.ToUpper(m[1:])
	})
	return res
}

func main() {
	s := "the-stealth-warrior"
	fmt.Println(ToCamelCase(s))
}

See the Go playground.

The output is theStealthWarrior.

The [-_]\w pattern matches a - or _ and then any word char. If you want to exclude _ from \w, use [^\W_] instead of \w.

huangapple
  • 本文由 发表于 2022年8月10日 19:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73305148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定