How to strip all non alphanumeric characters from a string in c++?

后端 未结 11 1453
野的像风
野的像风 2020-11-30 02:07

I am writing a piece of software, and It require me to handle data I get from a webpage with libcurl. When I get the data, for some reason it has extra line breaks in it. I

11条回答
  •  旧巷少年郎
    2020-11-30 02:41

    Just extending James McNellis's code a little bit more. His function is deleting alnum characters instead of non-alnum ones.

    To delete non-alnum characters from a string. (alnum = alphabetical or numeric)

    • Declare a function (isalnum returns 0 if passed char is not alnum)

      bool isNotAlnum(char c) {
          return isalnum(c) == 0;
      }
      
    • And then write this

      s.erase(remove_if(s.begin(), s.end(), isNotAlnum), s.end());
      

    then your string is only with alnum characters.

提交回复
热议问题