C++/Regarding String Manipulation
Expert: Ralph McArdell - 5/22/2008
QuestionHow to copy characters after the last occurence of delimeter from a string.
Eg:think am having the original string like "this/will/be/ur/great/day/goodmorning"
in output i want to copy the string "goodmorning" ie, after last ocuurence of the delimiter "/".
can u please suugest valuable solution for this.
thanks alot in advance.
AnswerHow about some logic like so (in pseudo code):
delimiter : constant character = '/'
begin : string position type = 0
end : string position type = nopos // nopos represents no position
// e.g. std::string::npos
repeat:
set end to position of next delimiter in original string starting from begin position
if delimiter found
set section string as copies of characters from begin position up to
but not including end position from original string
begin = end + 1 // skip over delimiter
end if
while delimiter found
// ### collect last part of original string ###
if begin position is not past the last character of the original string
set section string as copies of characters from begin position to
the end of the original string
endif
Section strings are the sections of the original string between delimiters, or the last part after the last delimiter to the end of the string. I have not shown how you store these. I assume that if the begin position is one past the last character in the original string that the find operation returns not found rather than fails by e.g. throwing an exception!
Here is an example implementation using std::string which stores the sections of the original string as strings in a vector (single dimension array) [Note: I am assuming you have access to a fairly ISO standard C++ (and C++ library) implementation]:
#include <string> // for std::string
#include <vector> // for std::vector to store sections
#include <algorithm> // for std::copy
#include <iostream> // for std::cout
#include <iterator> // for std::ostream_iterator
int main()
{
typedef std::vector<std::string> StringSectionCollectionType;
std::string originalString("this/will/be/ur/great/day/goodmorning");
StringSectionCollectionType sections;
char const delimiter('/');
std::string::size_type begin(0);
std::string::size_type end(std::string::npos);
do
{
end = originalString.find( delimiter, begin );
if ( std::string::npos != end )
{
std::string section( originalString.substr(begin, end-begin) );
sections.push_back( section );
begin = end + 1;
}
}
while ( std::string::npos != end );
// ### collect last part of original string ###
if ( begin < originalString.length() )
{
// Note: use from position to end of string form of substr operation
std::string section( originalString.substr(begin) );
sections.push_back( section );
}
std::copy
( sections.begin()
, sections.end()
, std::ostream_iterator<std::string>(std::cout, "\n")
);
return 0; // only required for certain broken compilers
}
The implementation above works with most variations of originalString - empty string, single "/" string, string with '/' as last character. You might wish to consider what should happen when a string contains more than one consecutive '/' e.g. "this/will//be/ur//great/day/goodmorning". Currently there will be an empty section string between each consecutive '/' so the example would yield sections: "this", "will", "", "be", "ur", "great", "day", "goodmorning".
Note that although I use the names begin and end these are not iterator types as returned by std::string::begin() and std::string::end(). Although using iterators would be nice the interface to std::string and its various operations makes it easier in this case to use character position values (and deduce a length for substr from them to boot!).
Although not immediately obvious we can replace the do .. while loop with a for loop in which the find is performed as the initial and part the loop update statements and the while condition is used as the for loop condition. We also have to update begin in the update part of the for loop. This the gives us a loop like so:
for
( end = originalString.find( delimiter, begin )
; std::string::npos != end
; begin = end + 1, end = originalString.find( delimiter, begin )
)
{
std::string section( originalString.substr(begin, end-begin) );
sections.push_back( section );
}
I use the C++ standard library copy algorithm together with an output stream iterator. The std::copy algorithm copies values from one sequence to another, while an output stream iterator adapts a std::ostream object to look like a iterator defined sequence. I use std::copy to copy the sequence of all items in the sections vector, from first to last, to std::cout with a newline between each item. The output should look like this:
this
will
be
ur
great
day
goodmorning
Another variation, using the original loop structure, would be to place the collection of the last section in the loop:
do
{
end = originalString.find( delimiter, begin );
if ( std::string::npos != end )
{
std::string section( originalString.substr(begin, end-begin) );
sections.push_back( section );
begin = end + 1;
}
else if ( begin < originalString.length() )
{
// ### collect last part of original string ###
std::string section( originalString.substr(begin) );
sections.push_back( section );
}
}
while ( std::string::npos != end );
But this does not really gain us very much, if anything at all, in the way of additional clarity or brevity.
You may be able to spend time to produce other variations on this theme but we will always have a special case to check for and handle when no further delimiters are found. It is a fact of life that these special cases tend to complicate and make less elegant otherwise simple procedures.Ho hum...
In any case I hope you find this of use.