Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Remove all special characters in a string
Qt 6.11 is out! See what's new in the release blog

Remove all special characters in a string

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 5.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kycho
    wrote on last edited by
    #1

    Hi. I would like to remove all special characters from the string.

    This is my code.

                str = str.simplified();
                str = str.remove("!");
                str = str.remove("@");
                str = str.remove("#");
                str = str.remove("$");
                str = str.remove("%");
                str = str.remove("^");
                str = str.remove("&");
                str = str.remove("*");
                str = str.remove("(");
                str = str.remove(")");
                str = str.remove("_");
                str = str.remove("+");
                str = str.remove("-");
                str = str.remove("=");
                str = str.remove(";");
                str = str.remove("'");
                str = str.remove(":");
                str = str.remove('"');
                str = str.remove("'");
                str = str.remove("/");
                str = str.remove(".");
                str = str.remove(",");
                str = str.remove("<");
                str = str.remove(">");
                str = str.remove("?");
                str = str.remove("[");
                str = str.remove("]");
                str = str.remove("{");
                str = str.remove("}");
                str = str.remove(" ");
    

    Of course it works well, but ... Is there more productive code?

    Thanks in advance for your advice.

    aha_1980A 1 Reply Last reply
    0
    • K Kycho

      Hi. I would like to remove all special characters from the string.

      This is my code.

                  str = str.simplified();
                  str = str.remove("!");
                  str = str.remove("@");
                  str = str.remove("#");
                  str = str.remove("$");
                  str = str.remove("%");
                  str = str.remove("^");
                  str = str.remove("&");
                  str = str.remove("*");
                  str = str.remove("(");
                  str = str.remove(")");
                  str = str.remove("_");
                  str = str.remove("+");
                  str = str.remove("-");
                  str = str.remove("=");
                  str = str.remove(";");
                  str = str.remove("'");
                  str = str.remove(":");
                  str = str.remove('"');
                  str = str.remove("'");
                  str = str.remove("/");
                  str = str.remove(".");
                  str = str.remove(",");
                  str = str.remove("<");
                  str = str.remove(">");
                  str = str.remove("?");
                  str = str.remove("[");
                  str = str.remove("]");
                  str = str.remove("{");
                  str = str.remove("}");
                  str = str.remove(" ");
      

      Of course it works well, but ... Is there more productive code?

      Thanks in advance for your advice.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kycho use a regular expression and http://doc.qt.io/qt-5/qstring.html#replace-12

      Qt has to stay free or it will die.

      1 Reply Last reply
      7
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Or, if you don't feel like bothering with regular expressions:

        auto it = std::remove_if(str.begin(), str.end(), [](const QChar& c){ return !c.isLetterOrNumber();});
        str.chop(std::distance(it, str.end()));
        
        1 Reply Last reply
        7

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved