0xAX
0xAX

Reputation: 21837

Split text by whitespaces

I have text file with some text information and i need to split this text at spaces and all word push into List.

I make so:

 QStringList list = line.split(" ");

  for (int i = 0; i < list.count(); i++){

      table.push_back(list[i]);
      this->ui->textEdit->setText(list[i]);
  }

In line i have my text. But when i test this code i get all text, but not by the word.

Thank you.

Upvotes: 1

Views: 6651

Answers (2)

Lance Diduck
Lance Diduck

Reputation: 1561

istream will already split according to whitespace. So an easy way to do this is

std::istream & txttosplit=X;///X is istringstream, or ifstream, or cin, etc
std::vector<std::string> words;
std::copy(std::istream_iterator<std::string>(txttosplit),
          std::istream_iterator<std::string>(),
          std::back_inserter(words));

Upvotes: 1

NomeN
NomeN

Reputation: 17613

Try it with:

line.split(QRegExp("\\s"));

Upvotes: 12

Related Questions