我想生成一个带有循环的返回字符串函数,用于一些索引字符串。

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

i want to generate return string function with looping for some index string

问题

func change(a string) string {
	// fmt.Println(a)
	v := ""
	if string(a) == "a" {
		return "A"
		v += a
	}
	return ""
}

func main() {
	fmt.Println(change("a"))
	fmt.Println(change("ab"))
}

我是你的中文翻译,以下是代码的翻译:

func change(a string) string {
	// fmt.Println(a)
	v := ""
	if string(a) == "a" {
		return "A"
		v += a
	}
	return ""
}

func main() {
	fmt.Println(change("a"))
	fmt.Println(change("ab"))
}

你说输出结果现在是"A",但是为什么当你将变量的值改为"ab"时,它返回了空值,而不是"Ab"作为输出呢?

英文:
func change(a string) string {
	// fmt.Println(a)
	v := ""
	if string(a) == "a" {
		return "A"
		v += a
	}
	return ""
}

func main() {
	fmt.Println(change("a"))
	fmt.Println(change("ab"))

}

i'm new at go and programming actually,
the output now is A, but why when i change the variable value to "ab" it returns no value it must be "Ab" for the output

答案1

得分: 2

所以,你基本上想要将输入中的所有a改为A
目前,你只是检查整个字符串是否等于"a",而"ab"不等于"a"。因此,在第二种情况下,程序最终返回""

通常情况下,你可以使用类似strings.ReplaceAll("abaaba","a","A")的方法实现你想要的效果。但出于教育目的,这里有一个"手动"解决方案。

func change(a string) string {
	v := "" // 我们构建的新字符串,逐步构造它
	for _, c := range a { // 遍历所有字符
		if c != 'a' { // 如果不是"a" ...
			v += string(c) // ... 将其附加到新字符串v
		} else {
			v += "A" // 否则将"A"附加到新字符串v
		}
	}
	return v
}

还要注意,c的类型是rune,因此必须使用string(c)将其转换为string

编辑:正如评论中指出的,实际上这不是构建新string的最佳方式。除了从rune转换为string的麻烦之外,每次附加内容时我们都会创建一个新的string并丢弃旧的string。相反,我们希望只创建一个string - 在最后,当我们确切知道结果string的样子时。
因此,我们应该使用字符串构建器。为了避免混淆,这里有一个单独的示例:

func change(a string) string {
	var resultBuilder strings.Builder
	for _, c := range a { // 遍历所有字符
		if c != 'a' { // 如果不是"a" ...
			resultBuilder.WriteRune(c) // ... 将其附加到新字符串v
		} else {
			resultBuilder.WriteString("A") // 否则将"A"附加到新字符串v
		}
	}
	return resultBuilder.String() // 在一切都设置好之后,创建最终的字符串
}
英文:

So, you basically want all as in the input to be changed to A.
At the moment you just check whether the whole string equals "a", and "ab" isn't equal to "a". Therefore, the program ends up with return "" in the second case.

Normally, you can achieve what you want with something like strings.ReplaceAll("abaaba","a","A"). But for educational purposes, here's a "manual" solution.

func change(a string) string {
	v := "" // our new string, we construct it step by step
	for _, c := range a { // loop over all characters
		if c != 'a' { // in case it's not an "a" ...
			v += string(c) // ... just append it to the new string v
		} else {
			v += "A" // otherwise append an "A" to the new string v
		}
	}
	return v
}

Also note that c is of type rune and therefore must be converted to string with string(c).

Edit: As noted in the comments, in practise that wouldn't be the best way to construct a new string. Appart from the hassle with the conversion from rune to string, we create a new string everytime we append something and drop the old one. Instead, we want to create an string only once - at the very end, when we know exactly how the result string shall look like.
Therefore, we should use a string builder instead. To avoid confusion, here's a separate example:

func change(a string) string {
	var resultBuilder strings.Builder
	for _, c := range a { // loop over all characters
		if c != 'a' { // in case it's not an "a" ...
			resultBuilder.WriteRune(c) // ... just append it to the new string v
		} else {
			resultBuilder.WriteString("A") // otherwise append an "A" to the new string v
		}
	}
	return resultBuilder.String() // Create the final string once everything is set
}

huangapple
  • 本文由 发表于 2022年11月10日 01:06:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/74378726.html
匿名

发表评论

匿名网友

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

确定