在字符串中的单词之间添加字符。

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

Add character between words in a string

问题

这是我想要实现的一些示例代码:

  1. var names string
  2. names = "Adam Eve Noah"
  3. // 未知代码部分
  4. fmt.Println(names) // 需要输出 "Adam-Eve-Noah"

请注意,这只是一个示例代码,其中的"未知代码部分"需要你自己填写。

英文:

Here is some sample code of what I want to achieve

  1. var names string
  2. names = "Adam Eve Noah"
  3. //Unknown code here
  4. fmt.Println(names) // Output required is "Adam-Eve-Noah"

答案1

得分: 0

使用strings库的ReplaceAll方法

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. var names string
  8. names = "Adam Eve Noah"
  9. names = strings.ReplaceAll(names, " ", "-")
  10. fmt.Println(names)
  11. }
英文:

Use ReplaceAll method from strings library

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. var names string
  8. names = "Adam Eve Noah"
  9. names = strings.ReplaceAll(names," ", "-")
  10. fmt.Println(names)
  11. }

huangapple
  • 本文由 发表于 2021年11月16日 02:27:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/69979140.html
匿名

发表评论

匿名网友

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

确定