I have a std::string s=n8Name4Surname. How can I obtain in 2 strings the Name and the Surname? THX
            Asked
            
        
        
            Active
            
        
            Viewed 3,114 times
        
    0
            
            
        - 
                    1Well what is the format of the string? "John Doe"? "Doe, John"? "Intergalactic Ombudsman Doe, John"? – Skurmedel May 17 '11 at 10:49
- 
                    What do `n8` and `4` signify in `n8Name4Surname`? Are they delimiters? – Nawaz May 17 '11 at 10:49
- 
                    i don't know. This is what i receive after I print the name of an templateobject – just me May 17 '11 at 10:51
- 
                    `n8Name4Surname`? Is this a variable? What does it contain? – johnsyweb May 17 '11 at 10:51
- 
                    1See this [question.](http://stackoverflow.com/questions/236129/how-to-split-a-string) – cpx May 17 '11 at 11:03
- 
                    1You want name demangling: http://sources.redhat.com/binutils/docs-2.15/binutils/c--filt.html – sehe May 17 '11 at 11:26
5 Answers
3
            One way to do this is using Boost.Tokenizer. See this example:
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
int main()
{
    using namespace std;
    using namespace boost;
    string text="n8Name4Surname.";
    char_separator<char> sep("0123456789");
    tokenizer<char_separator<char> > tokens(text, sep);
    string name, surname;
    int count = 0;
    BOOST_FOREACH(const string& s, tokens)
    {
        if(count == 1)
        {
            name = s;
        }
        if(count == 2)
        {
            surname = s;
        }
        ++count;
    }
}
EDIT
If you put the results in a vector, its even less code:
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <algorithm>
#include <iterator>
#include <vector>
int main()
{
    using namespace std;
    using namespace boost;
    string text="n8Name4Surname.";
    char_separator<char> sep("0123456789");
    tokenizer<char_separator<char> > tokens(text, sep);
    vector<string> names;
    tokenizer<char_separator<char> >::iterator iter = tokens.begin();
    ++iter;
    if(iter != tokens.end())
    {
        copy(iter, tokens.end(), back_inserter(names));
    }
}
 
    
    
        Asha
        
- 11,002
- 6
- 44
- 66
- 
                    can i put instead name and surname a std::vector? could you give me an example? – just me May 17 '11 at 11:04
2
            
            
        You can detect numerical characters in the string using function isdigit(mystring.at(position), then extract substring between those positions.
See:
 
    
    
        ascanio
        
- 1,506
- 1
- 9
- 18
1
            
            
        Use Boost tokenizer with the digits 0-9 as delimiters. Then, throw away the string containing "n". It's overkill, I realize...
 
    
    
        jonsca
        
- 10,218
- 26
- 54
- 62
1
            
            
        Simple STL approach:
#include <string>
#include <vector>
#include <iostream>
int main()
{
    std::string s= "n8Name4Surname";
    std::vector<std::string> parts;
    const char digits[] = "0123456789";
    std::string::size_type from=0, to=std::string::npos;
    do
    {
        from = s.find_first_of(digits, from);
        if (std::string::npos != from)
            from = s.find_first_not_of(digits, from);
        if (std::string::npos != from)
        {
            to = s.find_first_of(digits, from);
            if (std::string::npos == to)
                parts.push_back(s.substr(from));
            else
                parts.push_back(s.substr(from, to-from));
            from = to; // could be npos
        } 
    } while (std::string::npos != from);
    for (int i=0; i<parts.size(); i++)
       std::cout << i << ":\t" << parts[i] << std::endl;
    return 0;
}
 
    
    
        sehe
        
- 374,641
- 47
- 450
- 633
0
            
            
        Mandatory Boost Spirit sample:
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
int main()
{
    std::string s= "n8Name4Surname";
    std::string::const_iterator b(s.begin()), e(s.end());
    std::string ignore, name, surname;
    using namespace boost::spirit::qi;
    rule<std::string::const_iterator, space_type, char()> 
        digit = char_("0123456789"),
        other = (char_ - digit);
    if (phrase_parse(b, e, *other >> +digit >> +other >> +digit >> +other, space, ignore, ignore, name, ignore, surname))
    {
        std::cout << "name = " << name << std::endl;
        std::cout << "surname = " << surname << std::endl;
    }
    return 0;
}
 
    
    
        sehe
        
- 374,641
- 47
- 450
- 633
 
    