Search code examples
c++stringtrimerase

How do I remove everything in a string prior to a specific word in C++?


SEE FINAL UPDATE FOR SOLUTION

This is for school, so I request hints, not blatant answers if possible.

I need to take the phrase "My Name current community college" and output "Current Community College" I already have a loop to capitalize current community college.

Here is basically what I have up to this point:

    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <string>

    using namespace std;

    int main()
    {
        string phrase = "My Name current community college";

        cout << phrase << endl << endl;

        for (int i = 0; i < phrase.length(); i++)
        {
            if (phrase[i] == ' ')
            {
                phrase[i + 1] = toupper(phrase[i + 1]);
            }
        }
    return 0;
    }

I've tried using str.find and str.erase but those seem limited in that they can only search for invididual characters. Is there a way I can search for 'Current' (which would be the name of my community college) and just erase everything before the occurrence of that word in the string?

UPDATE: Thanks to Sam I Am I was able to accomplish my task, here is what I have now:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string phrase = "My Name current community college";

    cout << phrase << endl << endl;

    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase[i] == ' ')
        {
            phrase[i + 1] = toupper(phrase[i + 1]);
        }
    }

    size_t pos = phrase.find("Current");
    string output = phrase.substr(pos);

    cout << output << endl << endl;
    system("PAUSE");
    return 0;

}

I'm obsessive though and just getting an A doesn't accomplish my goal. Is there a way to accomplish this task without having to create a new string, as in, keep my single string but remove everything prior to 'Current'.

FINAL UPDATE

I figured it out, I don't even need substrings at all. you can use str.find() combined with str.erase() to just erase everything up until the found word using size_t pos:

#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    string phrase = "My Name current community college";

    cout << phrase << endl << endl;

    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase[i] == ' ')
        {
            phrase[i + 1] = toupper(phrase[i + 1]);
        }
    }

    size_t pos = phrase.find("Current"); //find location of word
    phrase.erase(0,pos); //delete everything prior to location found

    cout << phrase << endl << endl;
    system("PAUSE");
    return 0;

}

You can also replace

size_t pos = phrase.find("Current");
phrase.erase(0,pos);

with just phrase = phrase.substr(phrase.find("Current")); and that works too.


Solution

  • you can use find to find the index to start from, you can use substr to select the portion you want to return.

    size_t position = str.find("Current");   
    
    string newString = phrase.substr(position);     // get from position to the end
    

    http://www.cplusplus.com/reference/string/string/substr/