Go语言程序:将字符转换为字符串
在本文中,我们将学习如何在 Go 编程语言中将字符转换为字符串。
字符 - Go 语言没有 char 数据类型,相反,Go 语言中的字符由 rune 数据类型表示。Rune 表示一个以 UTF-8 格式编码的字符值。rune 的大小为 32 位。
字符串 - 字符串数据类型用于存储一系列字符。它可以是文字或字母的形式。字符串变量的大小为 1 字节或 8 位。
有两种方法可以实现此结果。我们将在本文中讨论它们。
语法
func QuoteRune(r rune) string func string(n int) string
QuoteRune() 函数在 strconv 包中定义,用于将 rune 或字符值转换为字符串。此函数以 rune 值作为输入,并将其转换为返回字符串形式的输出。
算法
步骤 1 - 导入所需的包,命名为 fmt、strconv 和 reflect。
步骤 2 - 启动 main() 函数。
步骤 3 - 初始化一个 rune 类型的变量并为其赋值。
步骤 4 - 调用 QuoteRune() 方法并将 rune 变量作为参数传递给它。
步骤 5 - 将获得的结果存储在另一个字符串类型的变量中。
步骤 6 - 使用 fmt.Println() 函数将结果打印到屏幕上,以及变量的类型。
示例 1
使用 QuoteRune() 方法将字符转换为字符串的 Go 语言程序。
package main
import (
"fmt"
"reflect"
"strconv"
)
// fmt package allows us to print anything on the screen.
// strconv package allows us to use other pre-defined functions like QuoteRune().
// reflect package allows us to use methods that allow knowing the type of a variable.
// calling the main function
func main() {
// initializing and defining a rune variable and assigning a value to it.
var r rune = '☺'
// using the QuoteRune() function defined in strconv package to convert runr to
// string.
// storing the string obtained in a new variable
var s string = strconv.QuoteRune(r)
// printing the type and value of the rune character
fmt.Println("The given variable is of type rune and has value of", r)
// printing the type and value of the string obtained from the rune.
fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}
输出
The given variable is of type rune and has value of 9786 The obtained variable is of type string and has value of '☺'
描述
首先,我们需要导入 fmt、strconv 和 reflect 包。fmt 包允许我们在屏幕上打印任何内容,strconv 包允许我们使用其他预定义函数,如 QuoteRune(),而 reflect 包允许我们使用允许了解变量类型的方法。
调用 main() 函数,这是我们程序的起点。
现在初始化一个 rune 类型的变量并为其赋值。
接下来,我们需要调用 strconv 包中定义的方法,该方法将允许我们使用 QuoteRune() 方法将 rune 变量作为参数传递给此函数,并且此函数将在将其转换为字符串后返回 rune 的相应字符串等价物。
现在将获得的结果存储在另一个字符串类型的变量中。
使用 fmt.Println() 函数在屏幕上打印 rune 值,并在下一行打印获得的字符串等价物及其类型。
示例 2
使用 String() 函数将字符转换为字符串的 Go 语言程序。
package main
import (
"fmt"
"reflect"
)
// fmt package allows us to print anything on the screen.
// reflect package allows us to use methods that allow knowing the type of a variable.
func runesToString(runes []rune) (outString string) {
// don't need index so _
for _, v := range runes {
outString += string(v)
}
return
}
// calling the main funciton
func main() {
// initializing and defining a rune variable and assigning a value to it.
var r []rune = []rune{'a', 'b', 'z'}
// Calling the runesToString() function to convert array of runes to string.
// storing the string obtained in a new variable
var s string = runesToString(r)
// printing the type and value of the rune character
fmt.Println("The given variable is of type rune and has value of", r)
// printing the type and value of the string obtained from the rune.
fmt.Println("The obtained variable is of type", reflect.TypeOf(s), "and has value of", s)
}
输出
The given variable is of type rune and has value of [97 98 122] The obtained variable is of type string and has value of abz
描述
首先,我们需要导入 fmt、strconv 和 reflect 包。fmt 包允许我们在屏幕上打印任何内容,reflect 包允许我们使用允许了解变量类型的方法。
初始化一个 RuneToString() 函数以将 rune 数组转换为字符串。此函数将以包含 rune 的数组作为参数,并在成功转换后返回其相应的字符串等价物。
调用 main() 函数,这是我们程序的起点。
现在初始化一个 rune 类型的数组并为其赋值。
接下来,我们需要调用 RuneToString() 方法。
现在将获得的结果存储在另一个变量中。
使用 fmt.Println() 函数在屏幕上打印 rune 值,并在下一行打印获得的字符串等价物及其类型。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP