Home > Backend Development > Golang > Quickly learn to convert string to array in Go language

Quickly learn to convert string to array in Go language

王林
Release: 2024-03-12 22:27:03
Original
835 people have browsed it

Quickly learn to convert string to array in Go language

Quickly learn to convert strings to arrays in Go language

In Go language, conversion between strings and arrays is a common operation. Especially when processing data, you often encounter situations where you need to convert a string into an array. This article will introduce how to quickly learn to convert strings to arrays in Go language, so that you can easily deal with similar problems.

In the Go language, we can use the Split function provided by the strings package to split a string into an array according to the specified delimiter. The following is a sample code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "apple,banana,orange"
    arr := strings.Split(str, ",")
    
    fmt.Println(arr)

    for _, v := range arr {
        fmt.Println(v)
    }
}
Copy after login

In this code, we first define a string str, which contains the names of three fruits, using commas as delimiters. Then use the strings.Split function to separate the string str by commas to get an array arr containing the names of the fruits. Finally, we print out each fruit name by looping through the array.

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

[apple banana orange]
apple
banana
orange
Copy after login

As can be seen from the output result, the string is successfully converted into an array, and the string is split into each element of the array.

In addition to using the Split function provided by the strings package, we can also use the strings.Fields function to split the string according to spaces and obtain an array containing words. The following is a sample code:

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "hello world welcome to Go"
    arr := strings.Fields(str)
    
    fmt.Println(arr)

    for _, v := range arr {
        fmt.Println(v)
    }
}
Copy after login

In this code, we define a string str, which contains several words in a sentence, using spaces as separators. Then the string str is separated by spaces through the strings.Fields function to obtain an array arr containing words. Finally, we print out each word by looping through the array.

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

[hello world welcome to Go]
hello
world
welcome
to
Go
Copy after login

Through the above example code, I believe you have mastered the method of converting string to array in Go language. When you encounter similar problems, you can flexibly use the functions provided by the strings package to split and convert strings, making your code more efficient and concise.

I hope this article will be helpful to you, and I wish you make more progress in learning and using the Go language!

The above is the detailed content of Quickly learn to convert string to array in Go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template