如何在Golang字符串中索引字符?

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

How to index characters in a Golang string?

问题

如何获得一个"E"的输出而不是69?

package main

import "fmt"

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

Golang是否有将字符转换为字节和反之的函数?

英文:

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

package main

import "fmt"

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

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

答案1

得分: 225

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

package main

import "fmt"

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

输出:

e
e
界

阅读:

Go编程语言规范中的转换部分。

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

英文:

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,

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 Specification section on Conversions.

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

答案2

得分: 28

这个怎么样?

英文:

How about this?

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

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

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

答案3

得分: 17

可以通过切片来完成

package main

import "fmt"

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

**注意:**此解决方案仅适用于ASCII字符。

英文:

Can be done via slicing too

package main

import "fmt"

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

NOTE: This solution only works for ASCII characters.

答案4

得分: 16

你也可以尝试将其转换为字符串。

package main

import "fmt"

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

You can also try typecasting it with string.

package main

import "fmt"

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

答案5

得分: 8

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

英文:

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 %c format specification works for any integer type.

答案6

得分: 6

通用的将字符解释为字符串的解决方案是string("HELLO"[1])

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

英文:

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

Rich's solution also works, of course.

答案7

得分: 3

尝试使用索引获取字符的方法:

package main

import (
      "fmt"
      "strings"
)

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

Try this to get the charecters by their index

package main

import (
      "fmt"
      "strings"
)

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

答案8

得分: 2

字符是符文,所以要打印它们,你必须将它们转换回字符串。

fmt.Print(string("HELLO"[1]))

英文:

String characters are runes, so to print them, you have to turn them back into String.

fmt.Print(string("HELLO"[1]))

答案9

得分: -2

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

英文:

Another Solution to isolate a character in a string

package main
import &quot;fmt&quot;

   func main() {
        var word string = &quot;ZbjTS&quot;

       // 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 &lt; 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

答案10

得分: -5

解决方案将是:

 package main

 import "fmt"

func main() {
  str := "HELLO"
  string(str[0])//H
  string(str[1])//E
  string(str[2])//L
  string(str[3])//L
  string(str[4])//O
}
英文:

The solution will be :

 package main

 import &quot;fmt&quot;

func main() {
  str := &quot;HELLO&quot;
  string(str[0])//H
  string(str[1])//E
  string(str[2])//L
  string(str[3])//L
  string(str[4])//O
}

huangapple
  • 本文由 发表于 2013年2月22日 14:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/15018545.html
匿名

发表评论

匿名网友

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

确定