Sign in to follow this  

Combine string

This topic is 2952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There's boost::erase_all, like so:
std::string str = "4F 6E 67";
boost::erase_all(str, " ");
std::cout << str << std::endl;
If you don't use boost, then you'll probably just have to do it manually: copy characters to a new string one by one, skipping over the ones you don't want.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
If you don't use boost, then you'll probably just have to do it manually
You can still be terse using the the standard library:

std::string a("4F 6E 67");
std::string b;
std::remove_copy(a.begin(), a.end(), std::back_inserter(b), ' ');
std::cout << b;


Not as nice to look at without Boost but still easier than writing it manually.

Share this post


Link to post
Share on other sites
Thanks for the reply. I am doing it in tis way, but seems like it doesnt work.

int j(0);
CString hexNewOutput;
int DataLength = 0;

hexstrOutput="4F 6E 67";
DataLength = hexstrOutput.GetLength();
for ( int i = 0; i<DataLength; i++)
{
if (hexstrOutput[i]!=' ')
{
hexNewOutput[j] = hexstrOutput[i];
j++;
}
}

Any comment?

Share this post


Link to post
Share on other sites
No OOP, but you get the idea...

#include <stdio.h>

void MunchCharacter(char *str, char ch)
{
char *head = str;
char *tail = str;

while(*head != '\0')
{
if(*head != ch)
{
*tail++ = *head;
}

head++;
}

*tail = *head; // aka (*tail = '\0')
}

int main(int argc, char **argv)
{
char str[] = "AB C D E F";
char ch = ' ';

printf("Input: [%s]\n",str);
MunchCharacter(str,ch);
printf("Output: [%s]\n",str);

system("pause");
return 0;
}




[Edited by - bitshifter on November 12, 2009 12:28:29 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
If you don't use boost, then you'll probably just have to do it manually

Nope. Just use the remove-erase-idiom.

#include <algorithm>
#include <iostream>
#include <string>

int main()
{
std::string s = "4F 6E 67";
std::cout << s << std::endl;
s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
std::cout << s << std::endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Adrian99420
Thanks for the reply, so any suggestion?
Surely you can do this yourself from the various methods everyone has provided?

And why is this (And every other thread you've posted), so urgent?

Share this post


Link to post
Share on other sites

This topic is 2952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this