C
Carl R. Davies
I want to find a character within a string then copy from that character
to the end into another string and erase it from the original.
But I'm not sure which of the many algorithms I should use, I was
looking at erase_tail_copy and erase_last_copy but I'm not sure if they
are what I want.
This is how I currently do it:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string test_data = "www.abc123.com/index.php#ref";
std::string fragment;
const std::string::size_type nFrag = test_data.rfind( "#" );
if( std::string::npos != nFrag )
{
fragment = test_data.substr( nFrag+1 );
// fragment should contain ref (drop #)
test_data.erase( nFrag );
// www.abc123.com/index.php
}
std::cout << "Fragment: " << fragment << std::endl;
return EXIT_SUCCESS;
}
Any advice on any aspect of this snippet is welcome?
Thanks,
Carl.
to the end into another string and erase it from the original.
But I'm not sure which of the many algorithms I should use, I was
looking at erase_tail_copy and erase_last_copy but I'm not sure if they
are what I want.
This is how I currently do it:
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
std::string test_data = "www.abc123.com/index.php#ref";
std::string fragment;
const std::string::size_type nFrag = test_data.rfind( "#" );
if( std::string::npos != nFrag )
{
fragment = test_data.substr( nFrag+1 );
// fragment should contain ref (drop #)
test_data.erase( nFrag );
// www.abc123.com/index.php
}
std::cout << "Fragment: " << fragment << std::endl;
return EXIT_SUCCESS;
}
Any advice on any aspect of this snippet is welcome?
Thanks,
Carl.