# Combine string

## Recommended Posts

Hi, this is what i am doing: I got a hex string : "4F 6E 67" I wish to combine the string to make it without the spacing which is "4F6E67". How can I do that? Thanks in advance.

Codeka    1239
What language?

##### Share on other sites
C++, i need urgent help. Thanks.

##### Share on other sites
Codeka    1239
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 on other sites
bitshifter    113
Also, you could do it with two pointers and two loops.
This would save you from having to copy the string.

##### Share on other sites
Thanks for the reply. I can't use the erase function. I am quite new in C++ programming, can someone show me how to pick the character manually and then combine it again? Thanks in advance.

##### Share on other sites
Codeka    1239
Wait, is this related to your other post? If you didn't want spaces in the string to begin with, why are you using code that puts them there?

##### Share on other sites
dmatter    4825
Quote:
 Original post by CodekaIf 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 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 on other sites
dmatter    4825
Presumably the character indexing operator[] of CString doesn't resize the string? So doing this: hexNewOutput[j] when that string has as size of zero is a bad idea. You can probably use the += operator to append the character though.

##### Share on other sites
Thanks for the reply, so any suggestion?

##### Share on other sites
dmatter    4825
Sorry, I think I edited my post while you were replying:
Quote:
 Original post by dmatterYou can probably use the += operator to append the character though.
That's assuming your CString refers to the ATL/MFC CString class.

##### Share on other sites
bitshifter    113
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 on other sites
dmatter    4825
Quote:
 Original post by bitshifterAnd i couldnt get the plusplus symbol to appear. WTF?
++ will show up in your final post but not in the preview, it's just a bug in the forum software.

##### Share on other sites
bitshifter    113
Ahh, thanks for the tip.
I thought it was some special tag i needed.
I will edit my post for clarity.

##### Share on other sites
DevFred    840
Quote:
 Original post by CodekaIf 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 on other sites
Evil Steve    2017
Quote:
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 on other sites
owl    376
    char* str = "4F 6E 67", val[7];    val[0]=str[0];val[1]=str[1];val[2]=str[3];val[3]=str[4];val[4]=str[6];val[5]=str[7];val[6]=0;

##### Share on other sites
Thanks for the advice guys, I did it using += operator to append the character. Juz rushing for a project, so can't put too much time on programming details. Thanks anyway.