英文:
print a value of particular byte in Array of string in golang
问题
我是新手,想要打印字符串数组的每个字节。在下面的代码中,我想要依次打印值 'h', 'e', 'l', 'l', 'o',但是我无法做到。
func main() {
strslice := make([]string, 4, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
for i := 0; i < len(strslice[i]); i++ {
fmt.Printf("slice is %c \n", strslice[i])
}
}
英文:
I am new to go lang and I want to print the individual byte of array of string
as in below code I want to print the values 'h','e','l','l','o' once at a time but I am not able to do the same.
func main() {
strslice := make([]string, 4, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
for i := 0; i < len(strslice[i]); i++ {
fmt.Printf("slice is %c \n", strslice[i])
}
}
答案1
得分: 6
在Go语言中,字符字面量以UTF-8编码的字节序列形式存储在字符串中。ASCII码点(0x00..0x7F)占用一个字节,其他码点占用两到四个字节。要单独打印码点(字符),可以使用以下代码:
package main
import "fmt"
func main() {
strslice := make([]string, 5, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
strslice[4] = "Hello, 世界"
for _, s := range strslice {
for _, c := range s {
fmt.Printf("%c ", c)
}
fmt.Printf("\n")
}
}
输出结果:
h e l l o
g o
l a n g
w h a t s u p
H e l l o , 世 界
下面是UTF-8编码字节和字符之间的区别的示例代码:
package main
import "fmt"
func main() {
str := "Hello, 世界"
fmt.Println("Bytes:")
for i := 0; i < len(str); i++ {
fmt.Printf("'%c' ", str[i])
}
fmt.Printf("\n")
fmt.Println("Characters:")
for _, c := range str {
fmt.Printf("'%c' ", c)
}
fmt.Printf("\n")
}
输出结果:
Bytes:
'H' 'e' 'l' 'l' 'o' ',' ' ' '世' '界'
Characters:
'H' 'e' 'l' 'l' 'o' ',' ' ' '世' '界'
参考资料:
For statements, The Go Programming Language Specification
英文:
In Go, character literals are stored in a string as a variable-width sequence of UTF-8 encoded bytes. The ASCII code points (0x00..0x7F) occupy one byte. Other code points occupy two to four bytes. To print code points (characters) separately,
package main
import "fmt"
func main() {
strslice := make([]string, 5, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
strslice[4] = "Hello, 世界"
for _, s := range strslice {
for _, c := range s {
fmt.Printf("%c ", c)
}
fmt.Printf("\n")
}
}
Output:
h e l l o
g o
l a n g
w h a t s u p
H e l l o , 世 界
Here's an illustration of the difference between UTF-8 encoded bytes and characters,
package main
import "fmt"
func main() {
str := "Hello, 世界"
fmt.Println("Bytes:")
for i := 0; i < len(str); i++ {
fmt.Printf("'%c' ", str[i])
}
fmt.Printf("\n")
fmt.Println("Characters:")
for _, c := range str {
fmt.Printf("'%c' ", c)
}
fmt.Printf("\n")
}
Output:
Bytes:
'H' 'e' 'l' 'l' 'o' ',' ' ' 'ä' '¸' '' 'ç' '' ''
Characters:
'H' 'e' 'l' 'l' 'o' ',' ' ' '世' '界'
References:
答案2
得分: 3
一种可能的方法:
func main() {
strslice := make([]string, 4, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
for i := 0; i < len(strslice); i++ {
for j := 0; j < len(strslice[i]); j++ {
fmt.Printf("slice[%d] is %c \n", i, strslice[i][j])
}
}
}
演示。如你所见,每个 strslice
元素都在嵌套的 for
循环中迭代,使用自己的循环变量 (j
)。
在 strslice[i][j]
中,i
用于访问切片的一个元素(一个字符串),而 j
用于访问该字符串的特定字节。
请注意,这是 字节,而不是 字符 - 因为这正是被要求的。但是,如果你实际上想打印出字符串的每个 字符,请查看 @peterSO 的精彩答案 - 因为你很有可能是这样想的。
英文:
One possible approach:
func main() {
strslice := make([]string, 4, 5)
strslice[0] = "hello"
strslice[1] = "go"
strslice[2] = "lang"
strslice[3] = "whatsup"
for i := 0; i < len(strslice); i++ {
for j := 0; j < len(strslice[i]); j++ {
fmt.Printf("slice[%d] is %c \n", i, strslice[i][j])
}
}
}
Demo. As you see, each strslice
element is iterated in a nested for
loop, using its own loop variable (j
).
In strslice[i][j]
, i
is used to access an element of slice (a string), and j
is used to access a specific byte of this string.
Note that it's byte, not character - because that's exactly what has been asked. But check wonderful @peterSO's answer if you actually want to print out each character of the string - as there's a big chance you do. )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论