Golang 将字符转换为字符串

Golang 将字符转换为字符串

在本文中,我们将学习如何在Go编程语言中将字符转换为字符串。

字符 - Go语言没有char数据类型,相反,go语言中的字符由rune数据类型表示。Rune表示以UTF-8格式编码的字符值。符文的大小为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步 – 将结果存储在类型为string的另一个变量中。

  • 第6步 – 使用fmt.Println()函数将结果与变量的类型一起打印在屏幕上。

示例1

使用QuoteRune()方法将字符转换为字符串的Golang程序。

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()函数将字符转换为字符串的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()函数,这是我们程序的起点。

  • 现在初始化一个runes类型的数组并给它赋值。

  • 接下来我们需要调用RuneToString()方法。

  • 现在将获取的结果存储在一个单独的变量中。

  • 使用fmt.Println()函数将符文值打印在屏幕上,并在下一行打印出获取到的字符串等效值及其类型。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程