bahadirtr
bahadirtr

Reputation: 53

Reaching a specific word in a string

Hi I have a string like this:

word1--tab--word2--tab--word3--tab--word4--tab--word5--tab--word6

I need to extract the third word from the string. I thought of reading character by character and getting the word after reading the second tab. But I guess it is inefficient. Can you show me a more specific way please?

Upvotes: 1

Views: 1257

Answers (2)

Nim
Nim

Reputation: 33655

assuming "tab" is \t;

std::istringstream str(".....");
std::string temp, word;

str >> temp >> temp >> word;

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258548

std::string has the find method which returns an index. You can use

 find("--", lastFoundIndex + 1)

three times to find the start index of your word, a fourth time for the end index, and then use substr.

Upvotes: 5

Related Questions