Golang程序 演示在字符串中写入双引号的例子

Golang程序 演示在字符串中写入双引号的例子

在Golang中,有多种在字符串中打印双引号的技术。在这篇文章中,我们将通过各种例子来学习这些技术。

语法

func Quote(s string) string
Go

Quote() 函数存在于strconv()包中,用于在双引号中添加给定的字符串字面。该函数接受要被引用的字符串变量作为参数,并在添加双引号后返回该字符串。

func Sprintf(format string, a ...interface{}) string
Go

该函数返回一个格式化的字符串。它接受一些字符串格式的参数。第一个参数应该是一个字符串格式,后面是一个可变数量的参数。

func Join(s []string, sep string) string
Go

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)
}
Go

输出

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."
Go

例子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()
}
Go

输出

The given string is:
 Never give up

Printing the above string in double quotes as:
 "Never give up"
Go

例子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)
}
Go

输出

The given string is:
 This is a string with "double quotes" inside
Go

例子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)
}
Go

输出

The given string is:
 Prevention is better than cure

The final string obtained after placing it to quotations is:
 "Prevention is better than cure"
Go

例子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)
}
Go

输出

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

结语

我们已经成功地编译并执行了一个Go语言程序,展示了在字符串中写入双引号的例子。我们在这个程序中使用了不同的函数,包括sprint()、quote()、Join()等,以便将一个给定的字符串字头纳入引号中。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册