如何将字符串作为字符值访问

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

How to access string as character value

问题

我想遍历一个字符串并返回字符值。如何不返回每个字母的数字值,而是返回实际的字符?

现在我得到的输出是:

 0 72 72
 1 101 101
 2 108 108
 3 108 108
 4 111 111

我希望的输出是:

 0 h h
 1 e e
 2 l l
 3 l l
 4 o o

以下是修改后的代码:

 package main

 import "fmt"

 func main() {

 	str := "Hello"
    for i, elem := range str {
     	fmt.Println(i, string(str[i]), string(elem))
    }

	for _, elem := range str {
		fmt.Println(string(elem))
	}	
 }

希望对你有帮助!

英文:

http://play.golang.org/p/ZsALO8oF3W

I want to traverse a string and return the character values. How do I, not return the numeric values per each letter, and return the actual characters?

Now I am getting this

 0 72 72
 1 101 101
 2 108 108
 3 108 108
 4 111 111

My desired output would be

 0 h h
 1 e e
 2 l l
 3 l l
 4 o o

 package main

 import "fmt"

 func main() {

 	str := "Hello"
    for i, elem := range str {
     	fmt.Println(i, str[i], elem)
    }

	for elem := range str {
		fmt.Println(elem)
	}	
 }

Thanks,

答案1

得分: 58

【For语句】
对于字符串值,"range"子句会迭代字符串中的Unicode代码点,从字节索引0开始。在后续的迭代中,索引值将是字符串中连续的UTF-8编码代码点的第一个字节的索引,第二个值是rune类型,表示相应代码点的值。如果迭代遇到无效的UTF-8序列,第二个值将是0xFFFD,即Unicode替换字符,并且下一次迭代将在字符串中前进一个字节。

例如,

package main

import "fmt"

func main() {
    str := "Hello"
    for _, r := range str {
        c := string(r)
        fmt.Println(c)
    }
    fmt.Println()
    for i, r := range str {
        fmt.Println(i, r, string(r))
    }
}

输出:

H
e
l
l
o

0 72 H
1 101 e
2 108 l
3 108 l
4 111 o

参考链接:For语句

英文:

> For statements
>
> For a string value, the "range" clause iterates over the Unicode code
> points in the string starting at byte index 0. On successive
> iterations, the index value will be the index of the first byte of
> successive UTF-8-encoded code points in the string, and the second
> value, of type rune, will be the value of the corresponding code
> point. If the iteration encounters an invalid UTF-8 sequence, the
> second value will be 0xFFFD, the Unicode replacement character, and
> the next iteration will advance a single byte in the string.

For example,

package main

import "fmt"

func main() {
	str := "Hello"
	for _, r := range str {
		c := string(r)
		fmt.Println(c)
	}
	fmt.Println()
	for i, r := range str {
		fmt.Println(i, r, string(r))
	}
}

Output:

H
e
l
l
o

0 72 H
1 101 e
2 108 l
3 108 l
4 111 o

答案2

得分: 6

包 main
使用 Printf 来指示你想要打印字符。

import "fmt"

func main() {

        str := "Hello"
        for i, elem := range str {
                fmt.Printf("%d %c %c\n", i, str[i], elem)
        }

}
英文:

package main
Use Printf to indicate you want to print characters.

import "fmt"

func main() {

        str := "Hello"
        for i, elem := range str {
                fmt.Printf("%d %c %c\n", i, str[i], elem)
        }

}

答案3

得分: 2

你迭代字符串中的字符的方式是可行的(尽管str[i]和elem是重复的)。你已经拥有了正确的数据。

为了正确显示,你只需要使用正确的格式输出(即将其解释为Unicode字符而不是整数)。

将代码中的:

fmt.Println(i, str[i], elem)

改为:

fmt.Printf("%d %c %c\n", i, str[i], elem)

%c 是根据Printf文档表示相应Unicode代码点的字符:http://golang.org/pkg/fmt/

英文:

The way you are iterating over the characters in the string is workable (although str[i] and elem are the duplicative of each other). You have the right data.

In order to get it to display correctly, you just need to output with the right formatting (i.e. interpreted as a unicode character rather than an int).

Change:

fmt.Println(i, str[i], elem)

to:

fmt.Printf("%d %c %c\n", i, str[i], elem)

%c is the character represented by the corresponding Unicode code point per the Printf doc: http://golang.org/pkg/fmt/

huangapple
  • 本文由 发表于 2013年10月8日 01:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/19231506.html
匿名

发表评论

匿名网友

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

确定