1

I made a vector that stores each sentence from a file. However, I noticed that each vector is stored differently. For example, if the file was "hello bob. how are you. hey there."

I used

while(getline(mFile, str, '.'))

to get each sentence and

vecString.push_back(str + '.');    

to store each each sentence in the vector. So vector[0] would hold "hello bob.", vector[1] would hold " how are you.", and vector [3] would hold " hey there.". How do I get rid of the space in starting sentence of vector[2] and vector [3]?

user3239138
  • 143
  • 4
  • 17
  • possible duplicate of [What's the best way to trim std::string](http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) – Bill Lynch Mar 01 '14 at 23:31

3 Answers3

1

The Boost String Algorithms Library has trimming functions.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
1

There are many examples of this on stackoverflow. Have a look at these.

Removing leading and trailing spaces from a string

What's the best way to trim std::string?

Community
  • 1
  • 1
jtate
  • 2,612
  • 7
  • 25
  • 35
1

Strip leading (i.e. left) whitespace using:

std::string s("  String with leading whitespace.");
s.erase(0, s.find_first_not_of(" \t"));

In addition to ' ' and '\t' consider also '\r', '\n', '\v', and '\f'.

Petr Vepřek
  • 1,547
  • 2
  • 24
  • 35