print a value of particular byte in Array of string in golang

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

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] = &quot;hello&quot;
    strslice[1] = &quot;go&quot;
    strslice[2] = &quot;lang&quot;
    strslice[3] = &quot;whatsup&quot;
    for i := 0; i &lt; len(strslice[i]); i++ {
	    fmt.Printf(&quot;slice is %c \n&quot;, 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' ',' ' ' '世' '界' 

参考资料:

Unicode UTF-8 FAQ

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 &quot;fmt&quot;

func main() {
	strslice := make([]string, 5, 5)
	strslice[0] = &quot;hello&quot;
	strslice[1] = &quot;go&quot;
	strslice[2] = &quot;lang&quot;
	strslice[3] = &quot;whatsup&quot;
	strslice[4] = &quot;Hello, 世界&quot;
	for _, s := range strslice {
		for _, c := range s {
			fmt.Printf(&quot;%c &quot;, c)
		}
		fmt.Printf(&quot;\n&quot;)
	}
}

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 &quot;fmt&quot;

func main() {
	str := &quot;Hello, 世界&quot;
	fmt.Println(&quot;Bytes:&quot;)
	for i := 0; i &lt; len(str); i++ {
		fmt.Printf(&quot;&#39;%c&#39; &quot;, str[i])
	}
	fmt.Printf(&quot;\n&quot;)
	fmt.Println(&quot;Characters:&quot;)
	for _, c := range str {
		fmt.Printf(&quot;&#39;%c&#39; &quot;, c)
	}
	fmt.Printf(&quot;\n&quot;)
}

Output:

Bytes:
&#39;H&#39; &#39;e&#39; &#39;l&#39; &#39;l&#39; &#39;o&#39; &#39;,&#39; &#39; &#39; &#39;&#228;&#39; &#39;&#184;&#39; &#39;&#39; &#39;&#231;&#39; &#39;&#39; &#39;&#39; 
Characters:
&#39;H&#39; &#39;e&#39; &#39;l&#39; &#39;l&#39; &#39;o&#39; &#39;,&#39; &#39; &#39; &#39;世&#39; &#39;界&#39; 

References:

Unicode UTF-8 FAQ

For statements, The Go Programming Language Specification

答案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] = &quot;hello&quot;
	strslice[1] = &quot;go&quot;
	strslice[2] = &quot;lang&quot;
	strslice[3] = &quot;whatsup&quot;

	for i := 0; i &lt; len(strslice); i++ {
		for j := 0; j &lt; len(strslice[i]); j++ {
			fmt.Printf(&quot;slice[%d] is %c \n&quot;, 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. )

huangapple
  • 本文由 发表于 2014年4月3日 18:46:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/22835209.html
匿名

发表评论

匿名网友

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

确定