May 10th, 2009, 06:16 AM
#1
Removing characters from a string [Solved]
Okay, what I basically want to do is to remove the two last characters from my string witch is a
tab and a newline character, but I think that doesn't matter, because they are always at the end of
the string. I got a code already, but it doesn't work correctly.
Code:
string::size_type pos = 0;
while ((pos = txt.find_first_of("\n\t", pos) != string::npos))
txt.erase(pos, 1);
return txt;
It seems like that code removes everything except the first character.
Thanks in advance
Last edited by WirelessDice; May 10th, 2009 at 07:36 AM .
Reason: *solved*
May 10th, 2009, 06:41 AM
#2
Re: Removing characters from a string
Hello WirelessDice
check the while condition and see what value it returns on each iteration
bye~
May 10th, 2009, 06:50 AM
#3
Re: Removing characters from a string
Originally Posted by
potatoCode
Hello WirelessDice
check the while condition and see what value it returns on each iteration
bye~
I edited the code so I could see the return values.
Code:
cout<<"TEST!!!!\n";
string::size_type pos = 0;
while ((pos = txt.find_first_of("\n", pos) != string::npos))
{
cout<<pos<<"\n";
txt.erase(pos, 1);
}
cout<<"END TEST!!!\n";
The return values I got was something like this.
Code:
TEST!!!
1
1
1
END TEST!!!
And instead of getting "-1" I'll only get "-", pleas help!
Thanks in advance.
May 10th, 2009, 06:57 AM
#4
Re: Removing characters from a string
Originally Posted by
WirelessDice
I edited the code so I could see the return values.
Code:
cout<<"TEST!!!!\n";
string::size_type pos = 0;
while ((pos = txt.find_first_of("\n", pos) != string::npos))
{
cout<<pos<<"\n";
txt.erase(pos, 1);
}
cout<<"END TEST!!!\n";
The return values I got was something like this.
Code:
TEST!!!
1
1
1
END TEST!!!
And instead of getting "-1" I'll only get "-", pleas help!
Thanks in advance.
There goes your answer~
Since the return value is always 1,
the erase() always starts at that index position!
May 10th, 2009, 07:05 AM
#5
Re: Removing characters from a string
Originally Posted by
potatoCode
There goes your answer~
Since the return value is always 1,
the erase() always starts at that index position!
Well, okay.. So how do I fix it? Or else, just change the whole code to remove the two last
characters!
May 10th, 2009, 07:14 AM
#6
Re: Removing characters from a string
Originally Posted by
WirelessDice
Well, okay.. So how do I fix it? Or else, just change the whole code to remove the two last
characters!
That's a find solution if only the last two strings need to be removed unconditionally
or do something along the line of
Code:
while(1)
{
pos = txt.find_first_of("\n\t");
if(pos == string::npos)
break;
txt.erase(pos, 1);
}
xD
May 10th, 2009, 07:17 AM
#7
Re: Removing characters from a string
give *complete compilable* code. e.g. what is in txt ??
also, why use find_FIRST_of, when you want to find the \t\n at the end of the string? just use find_last_of.
most importantly:
Code:
pos = txt.find_first_of("\n", pos) != string::npos;
is the same as
Code:
pos = ( txt.find_first_of("\n", pos) != string::npos );
therefore, for what you have coded, pos will only ever be 1 (or whatever bool(true) gets cast to as a string::size_type) inside that while loop.
try this:
Code:
while ( string::npos != (pos = txt.find_first_of("\n")) )
{
...
}
Last edited by Amleto; May 10th, 2009 at 09:36 AM .
May 10th, 2009, 07:34 AM
#8
Re: Removing characters from a string
Originally Posted by
potatoCode
That's a find solution if only the last two strings need to be removed unconditionally
or do something along the line of
Code:
while(1)
{
pos = txt.find_first_of("\n\t");
if(pos == string::npos)
break;
txt.erase(pos, 1);
}
xD
Wow, Thanks! That actually worked!
Thread solved!
May 10th, 2009, 10:21 AM
#9
Re: Removing characters from a string
Originally Posted by
WirelessDice
Thread solved!
You should prefer to use standard algorithms wherever possible. All of that crap can be replaced by a one-liner involving std::remove_if and std::isspace :
Code:
txt.erase(std::remove_if(txt.begin(), txt.end(), &std::isspace), txt.end());
Boost's string algorithms library provides more elegant solutions, if you're interested.
May 10th, 2009, 11:29 AM
#10
Re: Removing characters from a string
Originally Posted by
Plasmator
You should prefer to use standard algorithms wherever possible. All of that crap can be replaced by a one-liner involving
std::remove_if and
std::isspace :
Code:
txt.erase(std::remove_if(txt.begin(), txt.end(), &std::isspace), txt.end());
Boost's string algorithms library provides more elegant solutions, if you're interested.
cant get it to compile
Code:
_FwdIt std::remove_if(_FwdIt,_FwdIt,_Pr)' : cannot use function template 'bool std::isspace(_Elem,const std::locale &)' as a function argument
I included <locale>, not a c library though :s looks like that version isnt a predicate.
I got it to work using isspace from #include <ctype.h> which isnt a member of std namespace. i probably shouldnt be including that file though?
Last edited by Amleto; May 10th, 2009 at 11:52 AM .
May 10th, 2009, 12:46 PM
#11
Re: Removing characters from a string
Originally Posted by
Amleto
I included <locale>, not a c library though :s looks like that version isnt a predicate.
I got it to work using isspace from #include <ctype.h> which isnt a member of std namespace. i probably shouldnt be including that file though?
The problem is that std::isspace is a function template. ::isspace from <ctype.h> is not a function template. If you want to stick with <cctype>, a simple solution is to implement your own function, possibly by overloading, that forwards the job to std::isspace.
May 11th, 2009, 01:53 AM
#12
Re: Removing characters from a string
Originally Posted by
Plasmator
You should prefer to use standard algorithms wherever possible.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks