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

  • Home
  • SEARCH
  • 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 936895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:23:11+00:00 2026-05-15T21:23:11+00:00

Would there be a better way to do this using alogrithm functions like std::find()?

  • 0

Would there be a better way to do this using alogrithm functions like std::find()?

std::string s = "012X012Y012";

//After erasing the numbers from beginning and end; the end result should be X012Y

size_t index = s.find_first_not_of("0123456789");
string::iterator iter_end = s.begin();
advance(iter_end, index);
s.erase(s.begin(), iter_end);

//Now std::string should be X012Y012

size_t index = s.find_last_not_of("0123456789")+1;
string::iterator iter_start = s.begin();
advance(iter_start, index);
s.erase(iter_start, s.end());

//Finally, std::string should be X012Y
  • 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-15T21:23:12+00:00Added an answer on May 15, 2026 at 9:23 pm

    Seems fine enough. I’d factor out your constant:

    const std::string Numbers = "0123456789";
    

    And re-use that. erase does allow you to use indices instead of iterators, so you can trim all that iterator stuff from your code. You should also check the results of your search, otherwise you’ll do weird things to your string:

    std::size_t start = s.find_first_not_of(Numbers);
    if (start != std::string::npos)
        s.erase(0, start);
    
    size_t end = s.find_last_not_of(Numbers);
    if (end != std::string::npos)
        s.erase(end + 1, std::string::npos);
    

    Lastly, I think you should trim the right before the left. If you trim the left first, during erase the remainder of the string needs to be copied and moved down. By trimming the right first, you eliminate some things to copy. Nothing major, I suspect, but might as well.

    (This is true of most sequence containers: erasing from some point to the end simply means getting rid of objects, while erasing from the start to some point requires moving the remaining items into their new spot.)


    Of course, you can generalize this and re-use it:

    // typically you trim whitespace, so for the sake of
    // a generic utility, default to that
    const std::string whitespace = " \f\n\r\t\v";
    
    void trim_left(std::string& pStr, const std::string& pExcess = whitespace)
    {
        std::size_t start = s.find_first_not_of(pExcess);
        if (start != std::string::npos)
            s.erase(0, start);
    }
    
    void trim_right(std::string& pStr, const std::string& pExcess = whitespace)
    {
        size_t end = s.find_last_not_of(pExcess);
        if (end != std::string::npos)
            s.erase(end + 1, std::string::npos);
    }
    
    void trim(std::string& pStr, const std::string& pExcess = whitespace)
    {
        // right then left, for reasons said above
        trim_right(pStr, pExcess);
        trim_left(pStr, pExcess);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this by storing the URLs for the… May 16, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer I would recommend http://www.html5rocks.com/ by google for more info read… May 16, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer Thanks for the comments Till/Jon, Ive managed to fix this… May 16, 2026 at 11:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like to do something like this from a Ruby script, within a
I am building an automation system using Powershell and I would like to define
This seems like such a simple question, but I'm having such difficulty with it.
I have some functions that can be grouped together, but don't belong to some
I am using a TMemo to hold received characters from a serial port for
I have started to do some programming using VIM. I have very mixed feelings
I have a class that processes a 2 xml files and produces a text
I wrote some code to automatically track many objects through an 1000+ frame image
I was here yesterday and got some really great answers. I took what I
I apologize for being a bit verbose in advance: if you want to skip

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.