在Go语言中,我们有各种数据类型来存储数据,字符串数据类型是用来存储字符序列的。字符串变量的大小为1或8位。另一方面,数组是一种数据类型,用于在连续的内存位置存储数据。
语法
func fields(s string) []string
fields() 函数存在于字符串包中,用于将一个字符串转换为一个字符串数组。该函数接收一个字符串值作为参数,并返回相应的数组作为结果。
func split(s, sep string) []string
函数 split() 将两个字符串作为参数,e是我们希望分割的字符串,另一个是我们希望分割字符串s的字符或字母。
func typeofobject(x interface{})
typeof() 函数被用来获取任何变量的类型。这个函数存在于reflect包中,它将需要确定其类型的变量作为参数。然后,该函数返回作为结果的指定变量的类型。
方法1:使用Fileds()函数
在这个方法中,我们将使用一个叫做Fields()的内建函数。
算法
- 第1步 – 首先,我们需要定义包main。
-
第2步 – 然后导入软件包fmt和strings。
-
第 3 步 – 调用main()函数。
-
第4步 – 初始化和定义一个名为s的字符串,并给它赋值。此外,在屏幕上打印这个字符串
-
第5步 – 现在调用定义在字符串包中的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步 – 首先,我们需要定义包main并导入包fmt和strings。
-
第2步 – 接下来,调用main()函数。
-
第 3步– 初始化并定义一个名为s的字符串,并为其赋值,我们希望将其分割。初始化一个名为sep的空字符串变量并为其赋值。
-
第4步 – 现在,使用fmt.Println()函数在屏幕上打印该字符串。
-
第5步– 这将是分离字符串的值。在sep变量中存储””一个空格,因为我们希望以数组格式获得字符串的每个字。
-
第6步 – 现在调用string包中定义的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()预定义函数来完成这项任务。这些函数接受字符串作为参数,并在执行任务后返回相应的字符串数组。