Golang 将字符串转换为数组
在Go语言中,我们有各种数据类型来存储数据,String数据类型用于存储字符序列。字符串变量的大小为1或8位。另一方面,数组是一种数据类型,用于在相邻的内存位置存储数据。
语法
func fields(s string) []string
fields() 函数存在于strings包中,用于将一个字符串转换为字符串数组。这个函数以一个字符串值作为参数,并返回相应的数组作为结果。
func split(s, sep string) []string
函数 split() 接受两个字符串作为参数,一个为需进行拆分的字符串 s,另一个为我们希望通过其进行拆分 s 的字符或字母。该函数返回一个包含在拆分后得到的字符串的数组作为最后输出。
func typeofobject(x interface{})
typeof()函数用于获取任何变量的类型。该函数在reflect包中,它以要确定类型的变量作为参数。函数将返回指定变量的类型作为结果。
方法1:使用Fields()函数
在这种方法中,我们将使用一个名为Fields()的内置函数。
步骤
- 步骤1 - 首先,需要定义main包。
-
步骤2 - 然后导入fmt和strings包。
-
步骤3 - 调用main()函数。
-
步骤4 - 初始化并定义一个名为s的字符串,并对其赋值。然后在屏幕上打印这个字符串。
-
步骤5 - 现在调用strings包中定义的fields()函数,并将上面初始化的字符串作为参数传递给它。该函数通过执行转换返回结果。
-
步骤6 - 将函数返回的数组存储在一个单独的变量中,并使用fmt.Println()函数将结果打印在屏幕上。
示例
在这个示例中,我们将使用fields()函数将字符串转换为数组。
package main
import (
"fmt"
"reflect"
"strings"
)
func main() {
// initializing the string variable and assign value to it
var s string = "this is a sentence lets break it !"
fmt.Println("The given data is:\n", s, "and its type is", reflect.TypeOf(s))
arrayOfString := strings.Fields(s)
fmt.Println()
fmt.Println("The array obtained from the string is:\n", arrayOfString)
}
输出
The given data is:
this is a sentence lets break it ! and its type is string
The array obtained from the string is:
[this is a sentence lets break it !]
方法2:使用Split()函数
在这种方法中,我们将使用split()函数将数组转换为字符串。
步骤
- 步骤1 - 首先,我们需要定义package main并导入package fmt和strings。
-
步骤2 - 接下来,调用main()函数。
-
步骤3 - 初始化并定义一个名为s的字符串,将其赋予我们希望拆分的值。初始化一个名为sep的空字符串变量,并为其赋值。
-
步骤4 - 现在,使用fmt.Println()函数将字符串打印在屏幕上。
-
步骤5 - 这将是拆分s字符串的值。将一个空格“ ”存储在sep变量中,因为我们希望以数组格式获取字符串的每个单词。
-
步骤6 - 现在调用strings包中定义的split()函数,并将字符串以及sep变量作为参数传递给它。该函数将根据提供的变量拆分字符串并返回相应的数组。
-
步骤7 - 将函数返回的字符串数组存储在一个单独的变量中,并使用fmt.Println()函数将结果打印在屏幕上。
示例
以下是使用split()函数将字符串转换为数组的golang程序。
package main
import (
"fmt"
"reflect"
"strings"
)
func main() {
// initializing the string variable
var s string = "this is a sentence lets break it !"
var sep string = " "
fmt.Println("The given data is:\n", s, "and its data type is:", reflect.TypeOf(s))
arrayOfString := strings.Split(s, sep)
fmt.Println()
fmt.Println("The array obtained after splitting the string is:\n", arrayOfString)
}
输出
The given data is:
this is a sentence lets break it ! and its data type is: string
The array obtained after splitting the string is:
[this is a sentence lets break it !]
方法3:使用FOR循环
在这种方法中,我们将使用for循环将字符串转换为数组。
步骤
- 步骤1 - 首先,我们需要定义main包。
-
步骤2 - 然后导入fmt和reflect包。
-
步骤3 - 调用main()函数。
-
步骤4 - 初始化并定义名为s的字符串,并将其赋值。然后将这个字符串打印在屏幕上。
-
步骤5 - 现在使用for循环遍历数组,并将字符串的每个元素附加到一个新数组中。
-
步骤6 - 最后使用fmt.Println()函数将结果打印在屏幕上。
示例
以下是一个使用for循环将字符串转换为数组的Go语言程序。
package main
import (
"fmt"
"reflect"
)
func main() {
str := "hello world"
fmt.Println("The given data is:\n", str, "and its data type is:", reflect.TypeOf(str))
arr := make([]string, len(str))
for i, r := range str {
arr[i] = string(r)
}
fmt.Println()
fmt.Println("The array obtained by the above string is:",arr)
}
输出
The given data is:
hello world and its data type is: string
The array obtained by the above string is: [h e l l o w o r l d]
结论
我们已经成功编译并执行了一个Go语言程序,用于将字符串转换为数组,并附带了示例。我们在该任务中使用了strings库中定义的fields()和split()预定义函数。这些函数接受字符串作为参数,并在执行任务后返回相应的字符串数组。