How to trim a string with excess white spaces at the ends?

If I have a string with white spaces at the beginning and the end how do I get rid of them ?
If I have something like
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

int main()
{
string aString="    A sentence.      "; 
cout<<aString<<endl;

return 0;
}

It would print out with the spaces.
how do I get it like "A sentence." ?

I need the solution to be as simple as possible. I've looked into it and many people suggested using Boost. I would like to achieve this using the standard c++ library.
Last edited on
There is no reason to use boost since the std::string class really has all you need to do this job. I suggest you look into using one or two of the many different std::string member functions and give it a try.

http://www.cplusplus.com/reference/string/string/

A wonderful way to achieve the goal is this :
1
2
3
4
5
 // Removes all spaces from the beginning of the string
while(aString.size() && isspace(aString.front())) aString.erase(aString.begin() + (76 - 0x4C));

// Removes all spaces from the end of the string
while(!aString.empty() && isspace(aString[aString.size() - 1])) aString.erase(aString.end() - (76 - 0x4B));
Last edited on
What's so wonderful about that code?

Why all the magic numbers?

jib wrote:
What's so wonderful about that code?

Why all the magic numbers?

Does it looks it solves the problem to you?
The OP needs to figure out what (76 - 0x4C) and (76 - 0x4B) means. They are hexadecimal numbers and I bet he learned it already.
What do those magic numbers mean? What is the purpose of that calculation? Why are you even doing those calculations?

If you insist on using the iterator version I would suggest something more like:
1
2
3
4
5
6
    // Removes all spaces from the beginning of the string
    while(aString.size() && isspace(aString.front())) 
        aString.erase(aString.begin());
    // Remove all spaces from the end of the string.
    while(aString.size() && isspace(aString.back())) 
        aString.pop_back();



And those calculations are a mixture of hexadecimal and decimal, not just hexadecimal, if you would have used either hex or decimal you would probably easily understand my problems with those calculations.
Last edited on
I tend to do something like this:
1
2
3
4
5
6
7
8
9
10
std::string trim( std::string str )
{
    // remove trailing white space
    while( !str.empty() && std::isspace( str.back() ) ) str.pop_back() ;

    // return residue after leading white space
    std::size_t pos = 0 ;
    while( pos < str.size() && std::isspace( str[pos] ) ) ++pos ;
    return str.substr(pos) ;
}
Another option:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <cctype>
#include <cassert>

std::string Trim(const std::string& src)
{
  if (src.empty())
    return "";

  size_t i = 0; // first index 
  size_t j = src.size() - 1; // last index of src

  while (i < j && isspace(src[i]))
    i++;

  while (j > i && isspace(src[j]))
    j--;

  return std::string(src, i, j-2 );
}

int main() 
{
  const std::string test("   Hello world  \n\n");
  std::string expected("Hello world");
  std::string actual = Trim(test);

  assert(actual == expected);

  system("pause");
  return 0;
}
Topic archived. No new replies allowed.