Incrementation of char

前端 未结 3 557
青春惊慌失措
青春惊慌失措 2020-12-09 09:27

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

3条回答
  •  臣服心动
    2020-12-09 10:19

    If you're happy with the results that gives, then that's fine.

    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 '['.

    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".

提交回复
热议问题