How can I check if a string has special characters in C++ effectively?

前端 未结 9 845
情话喂你
情话喂你 2021-02-08 13:15

I am trying to find if there is better way to check if the string has special characters. In my case, anything other than alphanumeric and a \'_\' is considered a special charac

9条回答
  •  北恋
    北恋 (楼主)
    2021-02-08 13:57

    The first thing that you need to consider is "is this ASCII only"? If you answer is yes, I would encourage you to really consider whether or not you should allow ASCII only. I currently work for a company that is really having some headaches getting into foreign markets because we didn't think to support unicode from the get-go.

    That being said, ASCII makes it really easy to check for non alpha numerics. Take a look at the ascii chart.

    http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

    • Iterate through each character
    • Check if the character is decimal value 48 - 57, 65 - 90, 97 - 122, or 95 (underscore)

提交回复
热议问题