string 如何索引 Golang 字符串中的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15018545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:48:17  来源:igfitidea点击:

How to index characters in a Golang string?

stringgocharacter

提问by user977828

How to get an "E" output rather than 69?

如何获得“E”输出而不是 69?

package main

import "fmt"

func main() {
    fmt.Print("HELLO"[1])
}

Does Golang have function to convert a char to byte and vice versa?

Golang 是否具有将字符转换为字节的功能,反之亦然?

回答by peterSO

Interpreted string literals are character sequences between double quotes "" using the (possibly multi-byte) UTF-8 encoding of individual characters. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value identifying a Unicode code point. Therefore,

解释的字符串文字是双引号 "" 之间的字符序列,使用单个字符的(可能是多字节的)UTF-8 编码。在 UTF-8 中,ASCII 字符是与前 128 个 Unicode 字符对应的单字节字符。字符串的行为类似于字节片。符文是标识 Unicode 代码点的整数值。所以,

package main

import "fmt"

func main() {
    fmt.Println(string("Hello"[1]))              // ASCII only
    fmt.Println(string([]rune("Hello, 世界")[1])) // UTF-8
    fmt.Println(string([]rune("Hello, 世界")[8])) // UTF-8
}

Output:

输出:

e
e
界


Read:

读:

Go Programming Language Specificationsection on Conversions.

转到有关转换的编程语言规范部分。

The Go Blog: Strings, bytes, runes and characters in Go

Go 博客:Go 中的字符串、字节、符文和字符

回答by Rich Churcher

How about this?

这个怎么样?

fmt.Printf("%c","HELLO"[1])

As Peter points out, to allow for more than just ASCII:

正如彼得指出的那样,要允许的不仅仅是 ASCII:

fmt.Printf("%c", []rune("HELLO")[1])

回答by andybalholm

Go doesn't really have a character type as such. byte is often used for ASCII characters, and rune is used for Unicode characters, but they are both just aliases for integer types (uint8 and int32). So if you want to force them to be printed as characters instead of numbers, you need to use Printf("%c", x). The %cformat specification works for any integer type.

Go 并没有真正的字符类型。byte 通常用于 ASCII 字符,rune 用于 Unicode 字符,但它们都只是整数类型(uint8 和 int32)的别名。因此,如果您想强制将它们打印为字符而不是数字,则需要使用Printf("%c", x). 该%c格式规范适用于任何整数类型。

回答by Samkit Jain

Can be done via slicing too

也可以通过切片来完成

package main

import "fmt"

func main() {
    fmt.Print("HELLO"[1:2])
}

回答by Thomas Kappler

The general solution to interpreting a char as a string is string("HELLO"[1]).

将 char 解释为字符串的一般解决方案是string("HELLO"[1]).

Rich's solution also works, of course.

当然,Rich 的解决方案也有效。

回答by infiniteLearner

You can also try typecasting it with string.

您也可以尝试使用字符串对其进行类型转换。

package main

import "fmt"

func main() {
    fmt.Println(string("Hello"[1]))
}

回答by Anshu

Try this to get the charecters by their index

试试这个通过他们的索引来获取字符

package main

import (
      "fmt"
      "strings"
)

func main() {
   str := strings.Split("HELLO","")
    fmt.Print(str[1])
}

回答by H M C

Another Solution to isolate a character in a string

隔离字符串中字符的另一种解决方案

package main
import "fmt"

   func main() {
        var word string = "ZbjTS"

       // P R I N T 
       fmt.Println(word)
       yo := string([]rune(word)[0])
       fmt.Println(yo)

       //I N D E X 
       x :=0
       for x < len(word){
           yo := string([]rune(word)[x])
           fmt.Println(yo)
           x+=1
       }

}

for string arrays also:

对于字符串数组也:

fmt.Println(string([]rune(sArray[0])[0]))

// = commented line

// = 注释行