问题描述
我发现了一些问题,询问如何让 char 'B' 返回 'C' 然后 'D' 等.答案非常复杂,而且大多只是矫枉过正.
I found some question asking how to let char 'B' to return 'C' and then 'D' etc. The answers were quite complex and mostly just overkill.
为什么不简单地使用这个:
Why not to use simply this:
char X='A';
X++
从 A 到 Z,接下来呢?
It goes from A to Z and what next?
推荐答案
如果您对给出的结果感到满意,那很好.
If you're happy with the results that gives, then that's fine.
通常当我看到这样的问题时,他们希望从Z"换行到AA"或类似的东西——比如 Excel 列.显然只是增加一个 char 不会这样做 - 它会转到 '['.
Usually when I've seen questions like that, they want to wrap from "Z" to "AA" or something like that that though - like Excel columns. Clearly just incrementing a char won't do that - it would go to '['.
或者,即使在单个字符内,有效值的范围也可能是不连续的——最明显的例子是十六进制.如果你增加 '9' 你会得到 ':' 而不是你可能想要的 'a' 或 'A'.所需的顺序很少是Unicode 给你什么".
Alternatively, even within a single character, the range of valid values may be non-contiguous - the obvious example being hex. If you increment '9' you get ':' instead of the 'a' or 'A' which you probably wanted. The desired order is rarely "whatever Unicode gives you".
这篇关于char 的增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!