Trim a string
Mar 19, 2008 at 3:23pm UTC
Hi I'm new to c++ and I'm trying to trim a string from all the spaces it has. But so far I've had no luck at all.
So this is an example string " bla bla bla bla "
This should be the output of the method "blablablabla".
Does anyone know how to do this?
Thanks in advance Rope.
Mar 19, 2008 at 3:33pm UTC
std::remove(astring.begin(), astring.end(), ' ' );
Mar 19, 2008 at 5:08pm UTC
That doesn't work. leaves me with "test" but I entered "test test test". So it should be "testtesttest".
Any other ideas?
Mar 19, 2008 at 5:31pm UTC
This code works for me:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <string>
#include <algorithm>
int main(void )
{
std::string astring(" Test Test Test " );
std::remove(astring.begin(), astring.end(), ' ' );
std::cout << astring << std::endl;
return 0;
}
Last edited on Mar 19, 2008 at 5:32pm UTC
Mar 19, 2008 at 5:41pm UTC
Does it also apply when using cin to give astring a value?
Because your right when I use it to plainly display the string when the value is already given.
But when I use it like this:
1 2 3 4
string astring;
cin >> astring;
remove(astring.begin(), astring.end(), ' ' );
cout << astring << endl;
It just cout's "test" so it does remove the ' ', but it also delete's the other parts of the string.
Mar 19, 2008 at 6:11pm UTC
That's because cin stops reading at the " ", so the first "Test" is all that ever makes it into the string in the first place. Instead of cin, use getline:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string astring;
getline(cin,astring);
remove(astring.begin(),astring.end(),' ' );
cout<<astring<<endl;
return 0;
}
The getline function generally works much better when dealing with strings.
Last edited on Mar 19, 2008 at 6:18pm UTC
Mar 19, 2008 at 6:14pm UTC
@Rope: Try changing the second line to this:
Mar 19, 2008 at 6:39pm UTC
Give this a blast:
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
#include <iostream>
#include <string>
#include <algorithm>
int main(void )
{
std::string astring;
std::cout << "Enter Text:=" ;
std::cin >> astring;
std::cout << "Before trim:=" << astring << std::endl;
std::remove(astring.begin(), astring.end(), ' ' );
std::cout << "After trim:=" << astring << std::endl << std::endl;
std::cin.clear(); //clear flags
std::cin.ignore(256, '\n' ); //dump input stream;
std::string bstring;
std::cout << "Enter Text (getline):=" ;
std::getline(std::cin, bstring);
std::cout << "Before trim:=" << bstring << std::endl;
std::remove(bstring.begin(), bstring.end(), ' ' );
std::cout << "After trim:=" << bstring << std::endl;
return 0;
}
Mar 19, 2008 at 6:49pm UTC
Ok I tried to adjust the code to getlin(cin, astring). But nothing is happening.
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string astring;
getline(cin,astring);
remove(astring.begin(),astring.end(),' ' );
cout<<astring<<endl;
return 0;
system("pause" );
}
I enter: "test test test"
The output: ziltch, nada, nothing
system("pause") is there because of windows.
This is just ridiculous. I know how to do the whole thing with classes in header files etc. But a simple thing like this just won't work.
@ Grey wolf Tried your solution to. Doesn't work for me. The program just exits doing nothing.
Last edited on Mar 19, 2008 at 6:52pm UTC
Mar 19, 2008 at 7:24pm UTC
I have put your code into Dev-C++ and Visual Studio 2008 and it works in both (apart from system("pause") needs to be before return 0)!
What IDE/compiler are you using?
Mar 19, 2008 at 8:18pm UTC
@ grey wolf. Well. That did the trick. I relocated system("pause") above return 0; and now it works.
Thanks for the help!
P.S. Using Dev- C++
Mar 20, 2008 at 8:42am UTC
Hello,
Use this macro
#define CHARRTRIM(__x_) {int i; for (i=strlen(__x_) - 1; (i>=0)&&(__x_[i]==' '); i--); __x_[i + 1]=0;};
Mar 20, 2008 at 8:42am UTC
Hello,
Use this macro
#define CHARRTRIM(__x_) {int i; for (i=strlen(__x_) - 1; (i>=0)&&(__x_[i]==' '); i--); __x_[i + 1]=0;};
Mar 20, 2008 at 9:39am UTC
SteakRider
That does not do what was asked for! AND it's a macro
1 2 3 4 5
TO_PUT_IT_BLUNTLY: Macros are the bluntest instrument of C and C++'s abstraction
facilities, ravenous wolves in functions' clothing, hars to tame, marching to their
own beat all over your scopes. Aviod them.
Item 16 C++ Coding Standards
Herb Sutter, Andrei Alexandrescu
Topic archived. No new replies allowed.