How to remove first word from a string?

16,566

Solution 1

str=str.substr(str.find_first_of(" \t")+1);

Tested:

string sentence="Hello how are you.";
cout<<"Before:"<<sentence<<endl;
sentence=sentence.substr(sentence.find_first_of(" \t")+1);
cout<<"After:"<<sentence<<endl;

Execution:

> ./a.out
Before:Hello how are you.
After:how are you.

Assumption is the line does not start with an empty space. In such a case this does not work.

find_first_of("<list of characters>").

the list of characters in our case is space and a tab. This will search for first occurance of any of the list of characters and return an iterator. After that adding +1 movers the position by one character.Then the position points to the second word of the line. Substr(pos) will fetch the substring starting from position till the last character of the string.

Solution 2

Try the following

#include <iostream>
#include <string>

int main() 
{
    std::string sentence{"Hello how are you."};

    std::string::size_type n = 0;
    n = sentence.find_first_not_of( " \t", n );
    n = sentence.find_first_of( " \t", n );
    sentence.erase( 0,  sentence.find_first_not_of( " \t", n ) );

    std::cout << '\"' << sentence << "\"\n";

    return 0;
}

The output is

"how are you."

Solution 3

There are countless ways to do this. I think I would go with this:

#include <iostream>
#include <string>

int main() {
    std::string sentence{"Hello how are you."};

    // First, find the index for the first space:
    auto first_space = sentence.find(' ');

    // The part of the string we want to keep
    // starts at the index after the space:
    auto second_word = first_space + 1;

    // If you want to write it out directly, write the part of the string
    // that starts at the second word and lasts until the end of the string:
    std::cout.write(
        sentence.data() + second_word, sentence.length() - second_word);
    std::cout << std::endl;

    // Or, if you want a string object, make a copy from the start of the
    // second word. substr copies until the end of the string when you give
    // it only one argument, like here:
    std::string rest{sentence.substr(second_word)};
    std::cout << rest << std::endl;
}

Of course, unless you have a really good reason not to, you should check that first_space != std::string::npos, which would mean the space was not found. The check is omitted in my sample code for clarity :)

Share:
16,566

Related videos on Youtube

user3247278
Author by

user3247278

Updated on June 30, 2022

Comments

  • user3247278
    user3247278 almost 2 years

    Let's say I have

    string sentence{"Hello how are you."}
    

    And I want string sentence to have "how are you" without the "Hello". How would I go about doing that.

    I tried doing something like:

    stringstream ss(sentence);
    ss>> string junkWord;//to get rid of first word
    

    But when I did:

    cout<<sentence;//still prints out "Hello how are you"
    

    It's pretty obvious that the stringstream doesn't change the actual string. I also tried using strtok but it doesn't work well with string.

  • user3247278
    user3247278 over 9 years
    I'd prefer to use the string sentence variable again, not only print it the remaining string. I'm not too familiar with substr and not too clear on what you're doing with junkWord.length()+1 does.
  • pmr
    pmr over 9 years
    Have a look at Vlad's answer to see how to account for leading whitespace.
  • user3247278
    user3247278 over 9 years
    Thanks Vijay!! Ultimately everyone's answer seemed on point and I really appreciate everyone's help. Vijay your solution was the most simple and efficient and I appreciate your help. Can you quickly explain : find_first_of(" ")+1 thanks again!