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 8798451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:05:59+00:00 2026-06-14T00:05:59+00:00

Please tell me if I am understanding the the substr member function correctly? result

  • 0

Please tell me if I am understanding the the substr member function correctly?
result = result.substr(0, pos) + result.substr(pos + 1);
It takes the string from pos, 0 until (but not including), remove[i]
and then + result.substr(pos + 1); concatenates the rest of the string, except but not including the string / char in remove?

string removeLetters2(string text, string remove)
{
    int pos;
    string result = text;

    for (int i = 0; i < remove.length(); i++)
    {
        while (true)
        {
            pos = result.find(remove[i]);                
            if (pos == string::npos)                      
            {
                break;
            }
            else
            {
                result = result.substr(0, pos) +
                result.substr(pos + 1);
            }
        }
    }
    return result;
}
  • 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-06-14T00:06:00+00:00Added an answer on June 14, 2026 at 12:06 am

    In short, you are asking if

    result = result.substr(0, pos) +
             result.substr(pos + 1);
    

    removes the character at position pos, right?

    Short Answer:

    Yes.

    Longer Answer:

    The two-argument call takes the start index and the length (the one argument call goes to the end of string).

    It helps to imagine the string like this:

    F o o / B a r
    0 1 2 3 4 5 6   <- indices
    

    Now remove /:

    F o o / B a r
    0 1 2 3 4 5 6   <- indices
    1 2 3 |         <- 1st length
          | 1 2 3   <- 2nd length
    
    result = result.substr(0, 3)   <- from index 0 with length 3
           + result.substr(4);     <- from index 4 to end
    

    As a programmer, always be aware of the difference between distance/index and length.

    Better: If index is known:

    Your code creates two new, temporary strings, which are then concatenated into a third temporary string, which is then copied to result.

    It would be better to ask string to erase (wink wink) in place:

    result.erase(pos,1);
    // or by iterator
    string::iterator it = ....;
    result.erase(it,it+1);
    

    This leaves more optimization freedom to the string implementer, who may choose to just move all characters after pos by one to the left. This could, in a specialized scenario, be implemented with a single assignment, a single loop, and within the loop with the x86 swap instruction.

    Better: If characters to be deleted are known:

    Or, but I am not sure if this gives better performance, but it may give better code, the algorithm remove_if:

    #include <algorithm>
    
    // this would remove all slashes, question marks and dots
    ....
        std::string foobar = "ab/d?...";
        std::remove_if (foobar.begin(), foobar.end(), [](char c) {
            return c=='/' || c=='?' || '.';
        });
    

    remove_if accepts any function object.

    If there is just one character, it gets easier:

    // this would remove all slashes
    std::remove (foobar.begin(), foobar.end(), '/');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please tell me if I'm understanding correctly. (because I might not be.) User posts
I am not understanding what was the error here. Please tell me how to
Please tell, how can i determine, the browser supports canvas (paperjs), or not ?
Please tell me what is the Qt equivalent function for glutswapbuffers()..
Please tell me what will the call to given function return and how? The
Of course it's jQuery, and not my lack of understanding causing this issue, but
can you please tell me how to validate a hyperlink from different hyperlinks. eg
Can anyone tell me please how to extract the data from a .reg file
Please tell me there is a better solution to querying a remote Access database!
Please tell me will BlackBerry application till OS 7.1 work for new BlackBerry OS

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.