Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7530609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:57:28+00:00 2026-05-30T04:57:28+00:00

I need to replace characters in a QString based on their QChar::category . In

  • 0

I need to replace characters in a QString based on their
QChar::category. In stdlib terms I want to

string.erase(std::remove_if(begin(string), end(string), 
                            [](QChar c) { 
                            QChar::Category cat = c.category(); 
                            return cat == .... || cat == ...; }), 
             string.end());

Alternatively, I’m happy with a regexp that works on unicode character
categories that I can use for QString::replace.

Is that possible with QString or do I really need to turn the string
in a std::vector<QChar> and back?

Edit: The categories I want to keep:

  • for the first charater: $, _, or any character in the Unicode categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”
  • for the rest: the first bullet plus any U+200C zero width non-joiner characters, U+200D zero width joiner characters, and characters in the Unicode categories “Non-spacing mark (Mn)”, “Spacing combining mark (Mc)”, “Decimal digit number (Nd)”, or “Connector punctuation (Pc)”.

I can do first/rest in multiple passes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:57:29+00:00Added an answer on May 30, 2026 at 4:57 am

    Qt provides its own ways to do such things. Whether it is good, or not is doubtful, but Qt idiomatic would be

    QString result;
    result.reserve(string.size());
    foreach (const QChar& c, string) {
        if (is_good(c)) {
            result += c;
        }
    }
    

    Of course, you can do it with lambdas and std::for_each

    std::for_each(string.begin(), string.end(),
                      [&result](QChar c)
                        {
                            if (is_good(c)) {result += c; }
                        }
        );
    

    but it is not Qt idiomatic.

    Note, that removing symbols from a string is slower, then adding new, if space was reserved, that is why the first code sample is fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace special characters from a string, like this: this.value = this.value.replace(/\n/g,'');
I have a string. In this string i need replace all special characters (0-31
I need to replace certain ascii characters like @ and & with their hex
How can i replace this string of characters in javascript? I need all occurences
I need to replace character (say) x with character (say) P in a string,
I need help to replace all \n (new line) caracters for in a String,
I need to replace url invalid characters with something url valid then convert it
I need to replace all special control character in a string in Java. I
First question : I want to replace all characters other than alphanumeric and Special
In each column, after first 5 characters I need to add/replace next 5 characters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.