将字符串切割成字母。

huangapple go评论153阅读模式
英文:

Slice string into letters

问题

如何在Go语言中将一个字符串切割成包含其字符的字符串数组?

例如,将字符串"abc"切割成数组"a", "b", "c"。

英文:

How to slice one string in Go language into array of string letters it contains?

For example, turn string "abc" into array "a", "b", "c".

答案1

得分: 31

使用转换为符文(runes),例如

package main

import "fmt"

func main() {
    s := "Hello, 世界"
    for i, r := range s {
        fmt.Printf("i%d r %c\n", i, r)
    }
    fmt.Println("----")
    a := []rune(s)
    for i, r := range a {
        fmt.Printf("i%d r %c\n", i, r)
    }
}

Playground


输出:

i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r  
i7 r 世
i10 r 界
----
i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r  
i7 r 世
i8 r 界

从链接中:

将字符串类型的值转换为符文(runes)切片类型会生成一个包含字符串中各个Unicode码点的切片。如果字符串为空,则结果为[]rune(nil)

英文:

Use a conversion to runes, for example

package main

import "fmt"

func main() {
        s := "Hello, 世界"
        for i, r := range s {
                fmt.Printf("i%d r %c\n", i, r)
        }
        fmt.Println("----")
        a := []rune(s)
        for i, r := range a {
                fmt.Printf("i%d r %c\n", i, r)
        }
}

Playground


Output:

i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r  
i7 r 世
i10 r 界
----
i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r  
i7 r 世
i8 r 界

From the link:

> Converting a value of a string type to a slice of runes type yields a slice containing the individual Unicode code points of the string. If the string is empty, the result is []rune(nil).

答案2

得分: 22

使用strings.Split对其进行拆分:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%#v\n", strings.Split("abc", ""))
}

http://play.golang.org/p/1tNfu0iyHS

英文:

Use strings.Split on it:

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Printf("%#v\n",strings.Split("abc", ""))
}

http://play.golang.org/p/1tNfu0iyHS

答案3

得分: 12

我认为Split是你要找的:

func Split(s, sep string) []string

如果sep是一个空的字符串,它会将字符串分割成单个字符:

Split将字符串s分割成由分隔符sep分隔的所有子字符串,并返回这些分隔符之间的子字符串的切片。如果sep为空,则Split在每个UTF-8序列之后进行分割。它等同于带有计数-1的SplitN

英文:

I think Split is what you're looking for:

func Split(s, sep string) []string

If sep is an empty string, it will split the string into single characters:

> Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.

答案4

得分: 8

最简单的方法是分割字符串。

var userString = "some string to split"

newArr := strings.Split(userString, "") // 按每个字符进行分割

fmt.Println(newArr[2]) // 将打印出 "m"
英文:

The easiest way is to split.

var userString = "some string to split"

newArr:= strings.Split(userString, "")	     // split on each char 

fmt.Println(newArr[2])                       // will print "m"

huangapple
  • 本文由 发表于 2013年9月1日 16:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/18556693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定