4

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
user3247278
  • 179
  • 2
  • 3
  • 13
  • What about splitting the string up into words (via stringstream) and then readding all words except the first one? – arc_lupus Oct 01 '14 at 09:25
  • That would probably require that I use a while loop and make dynamic array of strings. Any easier way? – user3247278 Oct 01 '14 at 09:28
  • 1
    You only have to use a vector, no loops. Look at this: https://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – arc_lupus Oct 01 '14 at 09:29
  • @MatthiasB [try it](http://ideone.com/QUnx9a) – Dmitry Ledentsov Oct 01 '14 at 09:34
  • 1
    @DmitryLedentsov yes I noticed. I was thinking in the wrong direction. What is needed is of course `std::getline`: http://ideone.com/IxZRAt . Not that I would use this method in any case... – MatthiasB Oct 01 '14 at 09:40
  • Thanks everyone for your help, figured it out! – user3247278 Oct 01 '14 at 09:51
  • possible duplicate of [How to remove first word from a string?](http://stackoverflow.com/questions/17135452/how-to-remove-first-word-from-a-string) – Markus May 26 '15 at 15:21

8 Answers8

7

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."
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
4
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.

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Have a look at Vlad's answer to see how to account for leading whitespace. – pmr Oct 01 '14 at 09:49
  • 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! – user3247278 Oct 01 '14 at 09:50
1

You can for example take the remaining substring

string sentence{"Hello how are you."};
stringstream ss{sentence};
string junkWord;
ss >> junkWord;
cout<<sentence.substr(junkWord.length()+1); //string::substr

However, it also depends what you want to do further

Dmitry Ledentsov
  • 3,620
  • 18
  • 28
  • 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. – user3247278 Oct 01 '14 at 09:37
1

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 :)

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
0

You could use string::find() to locate the first space. Once you have its index, then get the sub string with string::substr() from the index after the index of the space up to the end of the string.

dandan78
  • 13,328
  • 13
  • 64
  • 78
0

One liner:

std::string subStr = sentence.substr(sentence.find_first_not_of(" \t\r\n", sentence.find_first_of(" \t\r\n", sentence.find_first_not_of(" \t\r\n"))));

working example:

#include <iostream>
#include <string>

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

    char whiteSpaces[] = " \t\r\n";

    std::string subStr = sentence.substr(sentence.find_first_not_of(whiteSpaces, sentence.find_first_of(whiteSpaces, sentence.find_first_not_of(whiteSpaces))));

    std::cout << subStr;

    std::cin.ignore();
}
Logman
  • 4,031
  • 1
  • 23
  • 35
0

Here's how to use a stringstream to extract the junkword while ignoring any space before or after (using std::ws), then get the rest of the sentence, with robust error handling....

std::string sentence{"Hello how are you."};
std::stringstream ss{sentence};
std::string junkWord;
if (ss >> junkWord >> std::ws && std::getline(ss, sentence, '\0'))
    std::cout << sentence << '\n';
else
    std::cerr << "the sentence didn't contain ANY words at all\n";

See it running on ideone here....

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0
#include <iostream>  // cout
#include <string>   // string
#include <sstream> // string stream
using namespace std;

int main()
{
     string testString = "Hello how are you.";

     istringstream iss(testString); // note istringstream NOT sstringstream

     char c; // this will read the delima (space in this case)
     string firstWord;

     iss>>firstWord>>c; // read the first word and end after the first ' '

    cout << "The first word in \"" << testString << "\" is \"" << firstWord << "\""<<endl;

    cout << "The rest of the words is \"" <<testString.substr(firstWord.length()+1) << "\""<<endl;
    return 0;
}

output

The first word in "Hello how are you." is "Hello"
The rest of the words is "how are you."

live testing at ideon

blongho
  • 1,151
  • 9
  • 12