Built-in function to convert an array of strings to a string

PHPz
Release: 2024-02-09 08:24:21
forward
545 people have browsed it

Built-in function to convert an array of strings to a string

php editor Banana introduces you a very convenient method - the built-in function to convert a string array into a string. During the development process, we often need to merge an array containing multiple strings into one string. In this case, we can use the implode() function. This function accepts two parameters, the first parameter is the separator used to join the array elements, and the second parameter is the array to be joined. By using the implode() function, we can quickly convert a string array into a string, which is convenient for us to use in subsequent processing. The use of this built-in function is very simple, you only need to pass in the corresponding parameters, which is very practical.

Question content

Suppose I have a string like this

stringArray := []string{"Hello"}

I want to convert the array to string so that

s := func(stringArray)

and s should be ["Hello"]

Is there a built-in function in golang that can do this for me?

Solution

Use json library:

import (
    "encoding/json"
)

func toString(arr []string) string {
    b, _ := json.Marshal(arr)
    return string(b)
}
Copy after login

See Live Demo.

The above is the detailed content of Built-in function to convert an array of strings to a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!