golang string to slice

WBOY
Release: 2023-05-10 09:55:36
Original
1420 people have browsed it

In golang, the string type is an immutable type, that is, its value cannot be modified directly. But in scenarios we sometimes need to convert the string type into a modifiable character sequence (slice). This article will introduce two methods to achieve this conversion.

Method 1: Use the strings package

The strings package is a string processing package in the golang standard library, which provides a method to convert strings to slices. The specific implementation is as follows:

import "strings"

func StringToSlice(s string) []rune {
    return []rune(s)
}
Copy after login

The rune type is used in this method. The rune type represents Unicode characters and can include characters in multiple languages ​​such as Chinese, Japanese, and Korean. By casting the string type to the rune type, you can get a modifiable character sequence (slice).

The following is a simple example showing the use of this method:

package main

import (
    "fmt"
    "strings"
)

func StringToSlice(s string) []rune {
    return []rune(s)
}

func main() {
    s := "hello world"
    sl := StringToSlice(s)
    sl[0] = 'H'
    fmt.Println(sl)
    fmt.Println(strings.ToUpper(string(sl)))
}
Copy after login

Program output:

[72 101 108 108 111 32 119 111 114 108 100]
HELLO WORLD
Copy after login

By passing the slice returned by the StringToSlice function to the strings.ToUpper function , we can convert the string to all uppercase letters.

Method 2: Using byte slice

In addition to using the strings package, we can also convert the string type into a byte slice, and then convert the byte slice into a rune type slice. The specific implementation is as follows:

func StringToSlice(s string) []rune {
    bs := []byte(s)
    rs := make([]rune, len(bs))
    for i, b := range bs {
        rs[i] = rune(b)
    }
    return rs
}
Copy after login

In this method, we use byte type slice. The byte type represents an 8-bit unsigned integer. By casting the string type to a byte type slice, we can get a sequence stored in bytes. We traverse the byte type slice, convert each element to rune type, and finally return a rune type slice.

The following is an example of using this method:

package main

import (
    "fmt"
)

func StringToSlice(s string) []rune {
    bs := []byte(s)
    rs := make([]rune, len(bs))
    for i, b := range bs {
        rs[i] = rune(b)
    }
    return rs
}

func main() {
    s := "hello world"
    sl := StringToSlice(s)
    sl[0] = 'H'
    fmt.Println(sl)
}
Copy after login

Program output:

[72 101 108 108 111 32 119 111 114 108 100]
Copy after login

Similar to method one, we can also convert the slice into a string, using strings The string operation function provided by the package implements operations such as string case conversion.

Conclusion

In general, we recommend using the API provided by the strings package for processing strings and character sequences, because it not only provides better performance, but also provides more Multiple types of security. But in some special cases, byte slice provides a more flexible processing method that can adapt to different needs.

The above is the detailed content of golang string to slice. For more information, please follow other related articles on the PHP Chinese website!

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