在Golang中如何找到下一个字符?

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

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 / 2x << 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 &#39;a&#39; &gt;&gt; 2 does is this:

  • 'a' is interpreted as int32(97) (example)
  • &gt;&gt; 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 &gt;&gt; 2) == (x / 4). (example)
  • 97 / 4 == 24. The b character has ASCII value 98. 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 &gt;&gt; y, we can note the following:

x(97):  01100001
y(2):   00000010
        -------- &gt;&gt;
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' (&lt;&lt;). Just like x &gt;&gt; 1 is the same as x / 2, x &lt;&lt; 1 is the same as x * 2.

Expression: 5&gt;&gt;1 == 5/2 == 2:

x(5):   00000101
y(1):   00000001
        -------- &gt;&gt;
z(2):   00000010

Expression: 5&lt;&lt;1 == 5*2 == 10:

x(5):   00000101
y(1):   00000001
        -------- &lt;&lt;
z(10):  00001010

Actually getting the next character

If you want the character directly following &#39;a&#39;, 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 &#39;a&#39;&gt;&gt;2 should work fine.

However to get the next character you can do something like:

func nextChar(ch byte) byte {
	if ch += 1; ch &gt; &#39;z&#39; {
		return &#39;a&#39;
	}
	return ch
}
func main() {
	fmt.Println(string(nextChar(&#39;a&#39;)))
}

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.

huangapple
  • 本文由 发表于 2015年3月29日 06:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/29323572.html
匿名

发表评论

匿名网友

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

确定