Golang程序 将字符转换为字符串
在这篇文章中,我们将学习如何将字符转换为字符串的Go编程语言。
字符 - Go语言没有char数据类型,相反,Go语言中的字符是由rune数据类型表示的。符文代表一个以UTF-8格式编码的字符值。符文的大小为32位。
字符串 - 字符串数据类型用于存储一连串的字符。它可以是字面的形式,也可以是字母的形式。字符串变量的大小为1字节或8位。
有两种方法来实现这一结果。我们将在本文中讨论它们。
语法
func QuoteRune(r rune) string
func string(n int) string
函数QuoteRune()定义于strconv包,用于将符文或字符值转换为字符串。该函数将符文值作为输入,并将其转换为字符串的输出。
算法
- 第1步 – 导入所需的包,命名为fmt、strconv和reflect。
-
第2步 – 启动main()函数。
-
第3步 – 初始化一个符文类型的变量并为其赋值。
-
第4步 – 调用QuoteRune()方法,并将rune变量作为参数传递给它。
-
第5步 – 将获得的结果存储在一个不同的字符串类型的变量中。
-
第6步 – 使用fmt.Println()函数在屏幕上打印结果以及变量的类型。
例子1
Golang程序使用QuoteRune()方法将字符转换为字符串。
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()函数,这是我们程序的起始点。
-
现在初始化一个符文类型的变量并给它赋值。
-
接下来我们需要调用strconv包中定义的方法,该方法允许我们使用QuoteRune()方法将符文变量作为参数传给该函数,该函数将在转换为字符串后返回符文的相应字符串等值。
-
现在将得到的结果存储在一个单独的字符串类型的变量中。
-
使用fmt.Println()函数在屏幕上打印符文值,并在下一行打印获得的字符串等价物及其类型。
例2
使用String()函数将字符转换为字符串的Golang程序。
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()函数,将符文阵列转换为字符串。这个函数将接受一个有符文的数组作为参数,并在成功转换后返回其对应的字符串等值。
-
调用main()函数,这是我们程序的起始点。
-
现在初始化一个符文类型的数组并给它赋值。
-
接下来我们需要调用RuneToString()方法。
-
现在将得到的结果存储在一个单独的变量中。
-
使用fmt.Println()函数在屏幕上打印符文值,并在下一行打印获得的字符串等值及其类型。
极客教程