关于boost里面的string

本文介绍Boost库中的字符串操作函数,包括大小写转换、查找、比较、分割与合并等实用功能。并通过示例代码展示了如何使用这些函数进行字符串处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <boost/algorithm/string.hpp>

首先是盘点子串是否是父串的一个子串。

如下所示:

    std::string str("I Don't Know.\n");
    std::cout << boost::to_upper_copy(str);
    std::cout << str ;
    boost::to_lower(str);
    std::cout << str;
    std::string str("Power Bomb");
    if (boost::iends_with(str, "bomb"))
    {
        std::cout << "bomb\n";
    }
    if (!boost::ends_with(str, "bomb"))
        std::cout << "no bomb\n" ;
    if (boost::starts_with(str, "Pow"))
        std::cout << "start Pow" << std::endl;
    if (boost::contains(str, "er"))
        std::cout << "contains er\n";
    std::string str2 = boost::to_lower_copy(str);
    if (boost::iequals(str, str2))
        std::cout << "iequal" << std::endl;
    std::string str3("power suit");
    if (boost::lexicographical_compare(str, str3))
    {
        std::cout << "litter\n";
    }
    if (boost::all(str2.substr(0, 5), boost::is_lower()))
        std::cout << "is low" << std::endl;
View Code

在这里有各种的查找和判断是否相等,如果前面加i说明是没有计较大小写。

还有一个很长的函数lexicographical_compare,该函数是判断左边的字符串是否小于右边的字符串。

boost::all(str2.substr(0, 5), boost::is_lower())

这个是判断all函数的第一个参数是否全部是小写的,如果是返回true

 

接下去是这个去除字符串头尾指定的字符的trim函数。如果不写if则默认是删除空格。如果有if责判断去除特定的字符。也可以删除多个特定的字符。如:boost::trim_copy_if(str2, boost::is_punct()||boost::is_digit())  去除符号和数字,并通过拷贝返回去,对str2不做改变。

 

对于erase和replace这两个函数也是差不多的道理,

std::cout << boost::ierase_all_copy(str, "samus");
std::cout << boost::replace_nth_copy(str, "l", 1, "L");
std::cout << boost::erase_tail_copy(str, 8);

 

接下来这个是关于分割的各种方式

加#include <boost/assign.hpp>

    std::string str = "Samus,Link.Zelda::Mario-Luigi+zelda";
    std::deque<std::string> d;
    boost::ifind_all(d, str, "zELDA");
    for (BOOST_AUTO(pos, d.begin()); pos != d.end(); ++pos)
    {
        std::cout << "[" << *pos << "]" ;
    }
    std::cout << std::endl;
    std::list<boost::iterator_range<std::string::iterator> > l;
    boost::split(l, str, boost::is_any_of(",.:-+"));
    for (BOOST_AUTO(pos, l.begin()); pos != l.end(); ++pos)
    {
        std::cout << "[" << *pos << "]" ;
    }
    std::cout << std::endl;
    l.clear();
    boost::split(l, str, boost::is_any_of(".:-"), boost::token_compress_on);
    for (BOOST_AUTO(pos, l.begin()); pos != l.end(); ++pos)
    {
        std::cout << "[" << *pos << "]";
    }
    std::cout << std::endl;
View Code

在分割的时候可以有多个字符来做个分隔符。并把分割后的字符串存储到一个容器中。如上面代码所示。

这里要提的一个是:boost::token_compress_on   使能压缩和另一个boost::token_compress_off的使能不压缩即在两个分隔符连在一起的时候如果使能压缩,则只看成是一个分隔符。

 

有分割就有合并

    std::vector<std::string> v = boost::assign::list_of("Samus")("Link")("Zelda")("Mario");
    std::cout << boost::join(v, "+") << std::endl;

    struct is_contains_a
    {
        bool operator()(const std::string &x)
        {
            return boost::contains(x, "a");
        }
    };
    std::cout << boost::join_if(v, "**", is_contains_a());
View Code

 

转载于:https://www.cnblogs.com/cxiaoln/p/3375304.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值