Go语言程序演示如何在字符串中写入双引号
在 Go 语言中,有多种技术可以打印带双引号的字符串。在本文中,我们将学习这些技术以及各种示例。
语法
func Quote(s string) string
Quote() 函数位于 strconv 包中,用于在给定字符串文字中添加双引号。该函数接受要加引号的字符串变量作为参数,并在添加双引号后返回字符串。
func Sprintf(format string, a ...interface{}) string
此函数返回格式化的字符串。它采用多个字符串格式的参数。第一个参数应为字符串格式,后跟可变数量的参数。
func Join(s []string, sep string) string
join 函数用于将数组转换为字符串。此函数位于 strings 包中。它接受两个参数,第一个是要转换的数组,第二个是将数组元素转换为字符串后应使用的分隔符,并返回最终字符串。
算法
步骤 1 - 首先,我们需要导入 fmt 包。
步骤 2 - 开始 main() 函数。
步骤 3 - 在 main() 函数内部,初始化一个字符串类型的变量并为其赋值。
步骤 4 - 然后将变量打印到屏幕上。
步骤 5 - 为了在双引号中包含此字符串,请使用 printf() 函数
示例 1
在这个例子中,我们将使用 printf() 以及 %q 格式化动词函数来打印带双引号的字符串。
package main
import "fmt"
func main() {
var str string= "You must be the change you wish to see in the world."
fmt.Println("The given string is:\n", str)
fmt.Println()
fmt.Printf("Printing the above string in double quotes as:")
fmt.Println()
fmt.Printf("%q\n", str)
}
输出
The given string is: You must be the change you wish to see in the world. Printing the above string in double quotes as: "You must be the change you wish to see in the world."
示例 2
在这个例子中,我们将编写一个 Go 语言程序,演示如何使用 strconv 包中的 Quote() 函数在字符串中写入双引号。
package main
import (
"fmt"
"strconv"
)
func main() {
var str string = "Never give up"
fmt.Println("The given string is:\n", str)
fmt.Println()
var result string = strconv.Quote(str)
fmt.Println("Printing the above string in double quotes as:\n", result)
fmt.Println()
}
输出
The given string is: Never give up Printing the above string in double quotes as: "Never give up"
示例 3
在这个例子中,我们将编写一个 Go 语言程序,演示如何使用反引号在字符串中写入双引号。使用反引号是另一种常用的方法,用于打印带引号的字符串。
package main
import (
"fmt"
)
func main() {
var str string = `This is a string with "double quotes" inside`
fmt.Println("The given string is:\n", str)
}
输出
The given string is: This is a string with "double quotes" inside
示例 4
在本文中,我们将编写一个 Go 语言程序,演示如何使用 sprint() 函数在字符串中写入双引号。
package main
import (
"fmt"
)
func main() {
var str string = "Prevention is better than cure"
fmt.Println("The given string is:\n", str)
fmt.Println()
var result string = fmt.Sprintf("%s", str)
fmt.Println("The final string obtained after placing it to quotations is:\n", result)
}
输出
The given string is: Prevention is better than cure The final string obtained after placing it to quotations is: "Prevention is better than cure"
示例 5
在本文中,我们将编写一个 Go 语言程序,演示如何使用 join() 函数在字符串中写入双引号。
package main
import (
"fmt"
"strings"
)
func main() {
var str string = "The way to get started is to quit talking and begin doing."
fmt.Println("The given string is:\n", str)
fmt.Println()
var result string = strings.Join([]string{""", str, """}, "")
fmt.Println("The final string obtained after placing it to quotations is:\n", result)
}
输出
The given string is: The way to get started is to quit talking and begin doing. The final string obtained after placing it to quotations is: "The way to get started is to quit talking and begin doing."
结论
我们已经成功编译并执行了一个 Go 语言程序,用于演示如何在字符串中写入双引号以及示例。我们在这个程序中使用了不同的函数,包括 sprint()、quote()、Join() 等,以便将给定的字符串文字包含在引号中。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP