英文:
How to find next character in Golang?
问题
我只是出于对golang的兴趣而做一些算法问题。我知道在其他语言中,要找到下一个按字母顺序排列的字符,可以对字符进行位移操作,因为字符(比如在C语言中)实际上是一个数字。
所以我尝试了以下代码:
"a" >> 2
或类似的操作,但是出现了类型不匹配的错误。
我想知道如何实现这个功能。
英文:
I'm just doing some algorithms problems out of interest in golang. I understand that in other languages to find the next character alphabetically I can bitshift the character, as a character (I'm thinking of C) is really a number.
So I tried doing
"a" >> 2
Or something to that effect, but there is a type mismatch.
I'd like to know how I can achieve this.
答案1
得分: 3
我不确定你从哪里得到这个想法,认为这会给你“下一个字符”。这在任何语言中都是不正确的。'a' >> 2
的作用如下:
'a'
被解释为int32(97)
(示例)>>
表示“将 X 向右移 Y 位”。将某个值向右移动 2 位与将其除以 4 的结果是相同的。因此,(x >> 2) == (x / 4)
(示例)97 / 4 == 24
。字符b
的 ASCII 值为98
。所以这并不能让你接近下一个字符(示例)
更多关于位移的内容
当考虑一个数的二进制表示时,位移操作最为明显。对于表达式 z = x >> y
,我们可以注意到以下内容:
x(97): 01100001
y(2): 00000010
-------- >>
z(24): 00011000
注意,x
中的所有位都向右移动了两位。末尾掉落的 1
被丢弃了。
类似地,你可以进行“左移”操作(<<
)。就像 x >> 1
等同于 x / 2
,x << 1
等同于 x * 2
。
表达式:5>>1 == 5/2 == 2
:
x(5): 00000101
y(1): 00000001
-------- >>
z(2): 00000010
表达式:5<<1 == 5*2 == 10
:
x(5): 00000101
y(1): 00000001
-------- <<
z(10): 00001010
实际获取下一个字符
如果你想要获取紧随 'a'
之后的字符,你只需要将其加 1,就像这个 示例 中所示。
英文:
I am not sure where you get the idea that this gives you the 'next character'. This is not true in any language. What 'a' >> 2
does is this:
- 'a' is interpreted as
int32(97)
(example) >>
means 'shift X right by Y bits'. Shifting something right by 2 bits is functionally the same as an integer divide by 4. So(x >> 2) == (x / 4)
. (example)97 / 4 == 24
. Theb
character has ASCII value98
. So this doesn't get you anywhere near. (example)
More on the bit shifting
Bit shifting is most obvious when considering a number in its binary notation. For the expression z = x >> y
, we can note the following:
x(97): 01100001
y(2): 00000010
-------- >>
z(24): 00011000
Note that all the bits in x
have simply been moved to the right by two bits. The 1
that fell off the end is dropped.
Similarly, you can 'shift left' (<<
). Just like x >> 1
is the same as x / 2
, x << 1
is the same as x * 2
.
Expression: 5>>1 == 5/2 == 2
:
x(5): 00000101
y(1): 00000001
-------- >>
z(2): 00000010
Expression: 5<<1 == 5*2 == 10
:
x(5): 00000101
y(1): 00000001
-------- <<
z(10): 00001010
Actually getting the next character
If you want the character directly following 'a'
, you simply add 1 to it as evidenced in this example.
答案2
得分: 1
你正在尝试移动一个字符串,而不是一个字节,就像@Not_a_Golfer所说的'a'>>2
应该可以正常工作。
然而,要获取下一个字符,你可以这样做:
func nextChar(ch byte) byte {
if ch += 1; ch > 'z' {
return 'a'
}
return ch
}
func main() {
fmt.Println(string(nextChar('a')))
}
当然,如果你需要的不仅仅是 a-z 的支持,那么会更加复杂。你可以查看 unicode
包和关于 Go 字符串的 这篇 博文。
英文:
You're trying to shift a string, not a byte, like @Not_a_Golfer said 'a'>>2
should work fine.
However to get the next character you can do something like:
func nextChar(ch byte) byte {
if ch += 1; ch > 'z' {
return 'a'
}
return ch
}
func main() {
fmt.Println(string(nextChar('a')))
}
Of course it'd be more complex if you need more than a-z support, take a look at the unicode
package and this blog post about go strings.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论