Home  >  Article  >  Backend Development  >  golang sting convert array

golang sting convert array

王林
王林Original
2023-05-10 10:38:061186browse

In recent years, golang has become increasingly popular in the programming field. It is easy to learn, has efficient concurrency performance and a powerful standard library. However, many programmers still face challenges when they need to convert a string into an array. In this article, we will introduce several methods to solve this problem, allowing you to complete the conversion between strings and arrays more conveniently.

Method 1: Use the Split function of the strings package

The strings package in golang provides many useful functions to process strings. Among them is the Split function, which splits a string according to the specified delimiter and returns a string slice. We can convert a string into an array by using this function. The sample code is as follows:

import (
    "fmt"
    "strings"
)

func main() {
    str := "a,b,c,d"
    arr := strings.Split(str, ",")
    fmt.Println(arr)
}

In the above code, we first define a variable str that contains multiple strings separated by commas. . Then, we use the Split function to separate the string by commas, and then store the split string slices in the variable arr. Finally, we output this string slice to the console through the Println function in the fmt package.

Run the above program, you will get the following output:

[a b c d]

As you can see, this function successfully converts a string into an array containing multiple strings.

Method 2: Use the Split function of the strconv package and the strings package

If you need to convert a string containing numbers into an integer array, we can first use the Split function of the strings package to convert the character Split the string according to the specified delimiter, and then convert the split string into an integer. This can be done using the Atoi function from the strconv package, which converts a string to an integer. The following is an example:

import (
    "fmt"
    "strconv"
    "strings"
)

func main() {
    str := "1,2,3,4"
    arrStr := strings.Split(str, ",")
    arr := make([]int, len(arrStr))
    for i, s := range arrStr {
        n, _ := strconv.Atoi(s)
        arr[i] = n
    }
    fmt.Println(arr)
}

In the above code, we first separate the strings containing numbers by commas, convert each separated string into an integer, and store it in an int type in the array. Finally, we output this array to the console through the fmt.Printf function.

Method 3: Use a for loop and the index function of the strings package

Another method is to use a for loop and the index function of the strings package to convert between strings and arrays. The sample code is as follows:

import (
    "fmt"
    "strings"
)

func main() {
    str := "a,b,c,d"
    arr := make([]string, len(str))
    for i, r := range str {
        if r == ',' {
            continue
        }
        arr[i] = string(r)
    }
    fmt.Println(arr)
}

In the above code, we once again define a variable str that contains multiple strings separated by commas. We then iterate over the string using a for loop and the index function of the strings package, storing each character into a string array. Since the index function returns the character's position in the string (rather than the character itself), we must use the string function to convert it to a string before we can store it in an array. If the current character is a comma, we skip it because we don't need to store the delimiter in the array. Finally, we output this string slice to the console through the Println function in the fmt package.

Summary:

In this article, we introduced several methods to convert strings to arrays, including using the Split function of the strings package, using the strconv package and the Split function of the strings package and Use a for loop and the index function of the strings package. No matter which method you choose, you can easily convert a string into an array and manipulate it further.

The above is the detailed content of golang sting convert array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn