-1

So I've looked up various functions and ways to remove whitespaces from strings, but none of them seem to be working for me. Here's what I have right now:

string filename = filenamet;
//remove all whitespace
//filename.erase(remove(filename.begin(), filename.end(), isspace), filename.end());

where filenamet is a string variable, and as is filename. I've double checked all my includes, so they don't seem to be the problem either. Here's the compiler error I'm getting:

test.cpp: In function ‘void input(char*, char**)’:
test.cpp:256:68: error: cannot convert ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’ filename.erase(remove(filename.begin(), filename.end(), isspace), filename.end());

I've also tried it with remove_if without remove, but then I get this compiler error:

test.cpp: In function ‘void input(char*, char**)’:
test.cpp:256:71: error: ‘remove_if’ was not declared in this scope
     filename.erase(remove_if(filename.begin(), filename.end(), isspace), filename.end());

Any help is greatly appreciated!

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Tingles
  • 23
  • 1
  • 6
  • Show the complete compilation error. What are your `#includes` ? – quantdev Aug 16 '14 at 20:35
  • possible duplicate of [What's the best way to trim std::string](http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) – Elliott Frisch Aug 16 '14 at 20:35
  • 1
    @ElliottFrisch OP seems to ask how to delete **all** spaces from a string, not how to trim spaces from the **ends** of a string. So, this is not a duplicate of *that* question. – eerorika Aug 16 '14 at 20:44
  • Thanks @cyber for editing my terrible formatting. – Tingles Aug 16 '14 at 21:02

4 Answers4

3

Always read your errors carefully!

test.cpp: In function ‘void input(char*, char**)’:
test.cpp:256:68: error: cannot convert 
    ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’ 
     to ‘const char*’ for argument ‘1’ 
     to ‘int remove(const char*)’ 
     filename.erase(remove(filename.begin(), filename.end(), isspace), filename.end());

The issue is that the compiler thinks you're calling this std::remove instead of this std::remove. I'm guessing the issue is that you simply forgot:

#include <algorithm>

because otherwise this line:

filename.erase(remove(filename.begin(), filename.end(), isspace), filename.end());

is the correct way to remove all spaces from the string filename.

Barry
  • 286,269
  • 29
  • 621
  • 977
1

You may try this also:

   string::iterator iter = filename.begin();
   while ( iter != filename.end() )
   {
      if ( isspace ( *iter ) )
      {
         iter = filename.erase ( iter );
      }
      else
      {
         iter++;
      }
   }

I compiled and tested it so it should run fine.

Vimal
  • 436
  • 1
  • 4
  • 14
0
#include <algorithm>
#include <cctype>

// One of this two:

s.resize(std::remove_if(s.begin(), s.end(), std::isspace) - s.begin());
s.erase(std::remove_if(s.begin(), s.end(), std::isspace), s.end());

Explanation

std::remove_if(s.begin(), s.end(), std::isspace) overwrites beggining of the s using s with whitespace characters skipped. It does not change size of string, so if there was any whitespace, end of the s is going to be untouched and contain provious content, which is likely to be useless, so we trim it out using std::string::resize or std::string::erase (std::remove_if return pass-the-end iterator to new content, which I used to determine what to erase / what should be the new size of s).

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
-1
std::string remove_char(const std::string &input, char to_remove){
    std::string output;
    for(unsigned i=0; i<input.size(); ++i)
        if(input[i]!=to_remove)
            output+=input[i];
    return output;
}
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
  • 1
    Actually, you're creating a new string which doesn't contain `to_remove` while that function is supposed to *remove* `to_remove` from `input`. – edmz Aug 17 '14 at 13:52