Golang 将字符串中的空格替换为特定字符

Golang 将字符串中的空格替换为特定字符

在Go编程语言中,字符串是一种内置的数据类型,用于表示字符序列。它们使用双引号(”)定义,并且可以包含任何有效的Unicode字符。这里首先使用的方法是Replace()函数,它将空格替换为特定字符,还可以应用许多其他方法来解决问题。使用fmt包将修改后的字符串输出打印到控制台。

方法1:使用strings.Replace()函数

在这种方法中,空格字符(” “)被替换为字符”-“,使用Replace()函数。该函数被指示替换所有出现的空格,最后输入的字符串为”-1″。如果你只想替换特定数量的出现次数,可以输入一个具体的数字。

语法

strings.Replace()

Replace()函数在Golang中用于将字符串中的特定子字符串的所有实例替换为另一个子字符串。函数需要三个输入:原始字符串、要更改的子字符串和替换的子字符串。它返回一个新的字符串,其中应用了所请求的替换。

步骤

  • 步骤1 - 在main包中创建一个程序,并声明fmt(格式化包)和strings包,主要用于生成可执行示例,fmt用于格式化输入和输出。

  • 步骤2 - 创建一个初始字符串,其中的空格将被一个特定字符替换。

  • 步骤3 - 使用Golang中的print语句在控制台上打印字符串。

  • 步骤4 - 使用strings.Replace()函数将空格替换为新字符。

  • 步骤5 - 使用fmt.Println()函数将修改后的字符串打印到控制台,其中ln表示换行。

示例

在下面的示例中,我们将使用内部字符串函数string.Replace()将字符串中的空格替换为特定字符。

package main
import (
   "fmt"
   "strings"    //import fmt and strings package
)

//create main function to execute the program
func main() {
   initial_string := "Hello alexa!"  //create string
   fmt.Println("The initial string created here is:", initial_string)
   replaced_string := strings.Replace(initial_string, " ", "-", -1)  //replace the space in string    using Replace() function
   fmt.Println("The replaced string after using replace method:")
   fmt.Println(replaced_string) //print the modified string on the console
}

结果

The initial string created here is: Hello alexa!
The replaced string after using replace method:
Hello-alexa!

方法2:使用for循环

在这个示例中,我们将学习如何用特定字符替换字符串中的空格。在这个方法中,我们使用for循环遍历原始字符串中的每个字符,判断当前字符是否为空格。如果是空格,则将替换字符添加到目标字符串中。如果不是空格,则将字符以原始形式添加到更改后的字符串中。运行该程序将得到与前一个示例相同的结果,即短语”Hello-alexa!”。

步骤

  • 步骤1 - 在程序的main模块创建一个main包,并声明 fmt (格式化包) 和 strings 包,其中 main 产生可执行的示例,fmt 用于格式化输入和输出。

  • 步骤2 - 创建一个main函数,在该函数中创建一个变量来保存要更改的起始字符串。

  • 步骤3 - 创建一个空白字符串变量,并将其初始化为保存更新后的字符串。

  • 步骤4 - 再次遍历起始字符串中的每个字符。

  • 步骤5 - 如果当前字符是空格,则将指定的替换字符添加到更改后的字符串中。

  • 步骤6 - 如果当前字符不是空格,则将字符添加到更改后的字符串中。

  • 步骤7 - 对原始字符串进行迭代,直到处理完所有字符。更改后的字符串现在包含了原始字符串中以请求的字符代替空格的结果。

  • 步骤8 - 使用 fmt.Println() 函数将更改后的字符串打印到控制台,其中 ln 表示新行。

示例

在下面的示例中,我们将使用一个用户定义的函数来替换golang中字符串的空格为一个特定的字符。

package main
import (
   "fmt"   //import fmt package in the program
)

//create main function to execute the program
func main() {
   initial_string := "Hello alexa!"  //create a string
   fmt.Println("The initial string created here is:", initial_string)
   var replaced_string string
   for _, ch := range initial_string {
      if ch == ' ' {
         replaced_string += "-"  //if the character is empty replace it with the specified character
      } else {
         replaced_string += string(ch) //else use the same character
      }
   }
   fmt.Println("The replaced string given here is:")
   fmt.Println(replaced_string) //print the modified string using print statement in Golang
}

输出

The initial string created here is: Hello alexa!
The replaced string given here is:
Hello-alexa!

结论

我们执行了使用两个示例将字符串中的空格替换为特定字符的程序。在第一个示例中,我们使用了内置的Replace()函数,而在第二个示例中,我们使用for循环来执行该程序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程