如何拆分包含字符和数字的字符串值

发布于 2024-11-07 17:27:28 字数 72 浏览 0 评论 0原文

我有一个 std::string s=n8Name4Surname。如何获取 2 个字符串中的姓名?谢谢

I have a std::string s=n8Name4Surname. How can I obtain in 2 strings the Name and the Surname? THX

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

徒留西风 2024-11-14 17:27:28

一种方法是使用 Boost.Tokenizer。请参阅此示例:

#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;
    }
}

编辑

如果将结果放入向量中,则代码甚至更少:

#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));
    }

}

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));
    }

}
红颜悴 2024-11-14 17:27:28

您可以使用函数 isdigit(mystring.at(position) 检测字符串中的数字字符,然后提取这些位置之间的子字符串。

请参阅:

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

You can detect numerical characters in the string using function isdigit(mystring.at(position), then extract substring between those positions.

See:

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

瑕疵 2024-11-14 17:27:28

使用 Boost 分词器,并以数字 0-9 作为分隔符。然后,丢弃包含“n”的字符串。我意识到这太过分了...

Use Boost tokenizer with the digits 0-9 as delimiters. Then, throw away the string containing "n". It's overkill, I realize...

¢好甜 2024-11-14 17:27:28

简单的STL方法:

#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;
}

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;
}
孤千羽 2024-11-14 17:27:28

强制精神提升样本:

#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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文