By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

String Algorithm Advice Please

P: n/a
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.
Mar 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Carl R. Davies wrote:
:: 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.

Never heard of those. :-)

::
:: 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?
::

It looks fine to me. Have you encountered any problems?
Bo Persson
Mar 24 '07 #2

P: n/a
Bo Persson wrote:
Carl R. Davies wrote:
:: 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.

Never heard of those. :-)

::
:: 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?
::

It looks fine to me. Have you encountered any problems?
The above does work. I was just wondering if there is a better way, some
way of replacing the conditional, substr and erase with one algorithm
call such as those I suggested.

Cheers,
Carl.
Mar 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.