Combine string
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.
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.
Also, you could do it with two pointers and two loops.
This would save you from having to copy the string.
This would save you from having to copy the string.
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.
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?
Quote:Original post by CodekaYou can still be terse using the the standard library:
If you don't use boost, then you'll probably just have to do it manually
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.
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!=' ')
{
hexNewOutput[j] = hexstrOutput;
j++;
}
}
Any comment?
int j(0);
CString hexNewOutput;
int DataLength = 0;
hexstrOutput="4F 6E 67";
DataLength = hexstrOutput.GetLength();
for ( int i = 0; i<DataLength; i++)
{
if (hexstrOutput!=' ')
{
hexNewOutput[j] = hexstrOutput;
j++;
}
}
Any comment?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement