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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:27:16+00:00 2026-05-14T05:27:16+00:00

i have a long string variable and i want to search in it for

  • 0

i have a long string variable and i want to search in it for specific words and limit text according to thoses words.

Say i have the following text :

“This amazing new wearable audio solution features a working speaker embedded into the front of the shirt and can play music or sound effects appropriate for any situation. It’s just like starring in your own movie“

and the words : “solution” , “movie”.

I want to substract from the big string (like google in results page):

“…new wearable audio solution features a working speaker embedded…”
and
“…just like starring in your own movie“

for that i’m using the code :

for (std::vector<string>::iterator it = words.begin(); it != words.end(); ++it)
{
    int loc1 = (int)desc.find( *it, 0 );    
    if( loc1 != string::npos )
    {
        while(desc.at(loc1-i) && i<=80)
        {       
            i++;
            from=loc1-i;
            if(i==80) fromdots=true;
        }
        i=0;
        while(desc.at(loc1+(int)(*it).size()+i) && i<=80)
        {
            i++;
            to=loc1+(int)(*it).size()+i;
            if(i==80) todots=true;
        }

        for(int i=from;i<=to;i++)
        {    
            if(fromdots) mini+="...";
            mini+=desc.at(i);
            if(todots) mini+="...";    
        }    
      }

but desc.at(loc1-i) causes OutOfRange exception… I don’t know how to check if that position exists without causing an exception !

Help please!

  • 1 1 Answer
  • 1 View
  • 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-14T05:27:17+00:00Added an answer on May 14, 2026 at 5:27 am

    This is an excellent exercise in taking advantage of what the STL has to offer. You simply open a reference and cherry-pick algorithms and classes for your solution!

    #include <iostream> // algorithm,string,list,cctype,functional,boost/assign.hpp
    using namespace std;
    
    struct remove_from {
        remove_from(string& text) : text(text) { }
        void operator()(const string& str) {
            typedef string::iterator striter;
            striter e(search(text.begin(), text.end(), str.begin(), str.end()));
            while( e != text.end() ) {
                striter b = e;
                advance(e, str.length());
                e = find_if(e, text.end(), not1(ptr_fun<int,int>(isspace)));
                text.erase(b, e);
                e = search(text.begin(), text.end(), str.begin(), str.end());
            }
        }
    private:
        string& text;
    };
    
    int main(int argc, char* argv[])
    {
        list<string> toremove = boost::assign::list_of("solution")("movie");
        string text("This amazing new wearable ...");
        for_each(toremove.begin(), toremove.end(), remove_from(text));
        cout << text << endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have long text . And I'm converting this string to dict. Here is
I have a long string, ex: Please help me to solve this problem. This
I have a long string, the format will be same of this string but
I have a very long String and I want to read character by character
I have a simple question. I have a long std::string that I want to
I have a variable x to which I want to assign a very long
Well i have a long string in double quotes var Variable = Really Long
I have this part of code. I want to put marker, when text in
I have a variable that contains a long string. (specifically it contains a few
I have a long string variable X and another string(a word or two in

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.