Golang 如何替换字符串中的字符
在Go语言中,字符串与其他语言如Java、C++、Python等不同。它是一个宽度可变的字符序列,每一个字符都由一个或多个使用UTF-8编码的字节表示。在Go字符串中,你可以使用给定的函数来替换给定字符串中的字符。这些函数是在strings包中定义的,所以你必须在你的程序中导入strings包来访问这些函数:
1.Replace: 该函数返回一个包含新字符串的副本,该字符串是通过替换旧字符串中的元素而创建的。如果给定的旧字符串是空的,那么它在字符串的开始部分进行匹配,在每个UTF-8序列之后,它最多产生m-rune字符串的m+1替换。如果n的值小于0,那么这个函数可以替换给定字符串中的任何数量的元素(没有任何限制)。
语法
func Replace(str, oldstr, newstr string, m int) string
这里, str 是原始字符串, oldstr 是你想替换的字符串, newstr 是替换 oldstr 的新字符串, n 是替换 oldstr 的次数。
例子
// Go program to illustrate how to
// replace characters in the given string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing strings
str1 := "Welcome to Geeks for Geeks"
str2 := "This is the article of the Go string is a replacement"
fmt.Println("Original strings")
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
// Replacing strings
// Using Replace() function
res1 := strings.Replace(str1, "e", "E", 3)
res2 := strings.Replace(str2, "is", "IS", -2)
res3 := strings.Replace(str1, "Geeks", "GFG", 0)
// Displaying the result
fmt.Println("\nStrings(After replacement)")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
}
输出
Original strings
String 1: Welcome to Geeks for Geeks
String 2: This is the article of the Go string is a replacement
Strings(After replacement)
Result 1: WElcomE to GEeks for Geeks
Result 2: ThIS IS the article of the Go string IS a replacement
Result 3: Welcome to Geeks for Geeks
2.ReplaceAll: 这个函数用来用一个新的字符串替换所有的旧字符串。如果给定的旧字符串是空的,那么它将在字符串的开始部分进行匹配,在每个UTF-8序列之后,它将产生最多M+1的M-rune字符串的替换。
语法
func ReplaceAll(str, oldstr, newstr string) string
这里, str是原始字符串, oldstr是你想替换的字符串, newstr是替换 oldstr的新字符串。让我们借助于一个例子来讨论这个概念:
例子
// Go program to illustrate how to
// replace characters in the given string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing strings
str1 := "Welcome to Geeks for Geeks"
str2 := "This is the article of the Go string is a replacement"
fmt.Println("Original strings")
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
// Replacing strings
// Using ReplaceAll() function
res1 := strings.ReplaceAll(str1, "Geeks", "GFG")
res2 := strings.ReplaceAll(str2, "the", "THE")
// Displaying the result
fmt.Println("\nStrings(After replacement)")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
}
// Go program to illustrate how to
// replace characters in the given string
package main
import (
"fmt"
"strings"
)
// Main function
func main() {
// Creating and initializing strings
str1 := "Welcome to Geeks for Geeks"
str2 := "This is the article of the Go string is a replacement"
fmt.Println("Original strings")
fmt.Println("String 1: ", str1)
fmt.Println("String 2: ", str2)
// Replacing strings
// Using ReplaceAll() function
res1 := strings.ReplaceAll(str1, "Geeks", "GFG")
res2 := strings.ReplaceAll(str2, "the", "THE")
// Displaying the result
fmt.Println("\nStrings(After replacement)")
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
}
输出
Original strings
String 1: Welcome to Geeks for Geeks
String 2: This is the article of the Go string is a replacement
Strings(After replacement)
Result 1: Welcome to GFG for GFG
Result 2: This is THE article of THE Go string is a replacement
极客教程