字符串切割等操作

static void SplitString1(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
    std::string::size_type posSubStringStart;   //子串起始位置

    std::string::size_type posSeparator;        //分隔符位置

    posSeparator = srcStr.find(separatpor);
    posSubStringStart = 0;
    while( std::string::npos != posSeparator )
    {
        std::string ss = srcStr.substr(posSubStringStart, posSeparator -posSubStringStart);
        if ( ss != "")
            vec.push_back(ss);

        posSubStringStart = posSeparator + separatpor.size();
        posSeparator = srcStr.find(separatpor, posSubStringStart);
    }

    if ( posSubStringStart != srcStr.length() )     //jiequ zuihou yiduan shuju
    {
        vec.push_back(srcStr.substr(posSubStringStart));
    }
}

static void SplitString2(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
    std::string::size_type pos;
    
    std::string srcStr_2 = srcStr + separatpor; //扩展字符串以方便操作
    int size = srcStr_2.size()
    
    for ( int i = 0; i < size; i++ )
    {
        pos = srcStr_2.find(separatpor, i);
        if ( pos < size )
        {
            std::string s = srcStr_2.substr(i, pos - i);
            i = pos + separatpor.size() -1;
            vec.push_back(s);
        }
    }
    
}


static void SplitString3(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
    std::string a;
	std::istringstream ss(srcStr);
    while( ss >> a )
    {
        vec.push_back(a);
    }
}

static void SplitString4(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{

    std::string line;
    std::string stringstream ss(srcStr);
    while( getline(ss, line, ' '))//按空格分割
    {
        if ( !line.empty() )
        {
            vec.push_back( line );
        }
        
    }
}


//去除字符串的首尾空格
static void TrimString(std::string& sSrc)
{
	if (sSrc.empty())
		return;

	sSrc.erase(0, sSrc.find_first_not_of(' '));
	sSrc.erase(sSrc.find_last_not_of(' ') + 1);
	sSrc.erase(0, sSrc.find_first_not_of('\r'));
	sSrc.erase(sSrc.find_last_not_of('\r') + 1);
	sSrc.erase(0, sSrc.find_first_not_of('\n'))
}

//移除空串
static void DropEmptrStr(std::vector<std::string>& vec)
{
	std::auto it = vec.begin();
	while (it != vec.end())
	{
		if (it->empty())
		{
			it = vec.erase(it);
		}
		else
		{
			++it;
		}
	}
}


//字符串拷贝,调用者负责内存释放
static char* strDump(const std::string& sSrc)
{
	size_t size = sSrc.size() + 1;
	char* dst = new char[size];
	strcpy_s(dst, size, sSrc.c_str());

	return dst;
}


//判断字符串中是否存在要寻找的字符串
static bool HasDstString(const std::string& sSrcStr, const std::string& sDstStr)
{
    std::auto pos = sSrcStr.find(sDstStr);
    if ( pos != std::string::npos)
    {
        return true;
    }

    return false;
}

//判断字符串中是否存在要寻找的字符串,无论大小写
static bool HasDstString(const std::string& sSrcStr, const std::string& sDstStr)
{
    std::string tmpsrc = boost::to_lower_copy(sSrcStr);
    std::string tmpDst = boost::to_lower_copy(sDstStr);
    std::auto pos = tmpsrc .find(tmpDst);
    if ( pos != std::string::npos)
    {
        return true;
    }

    return false;
}


int main()
{
    std::string str;
    std::cout << "Please input str: " << std::endl;
    getline(std::cin, str);
    std::string pattern;
    std::cout << "Please input pattern: " << std::endl;
    getline(std::cin, pattern);
    
    std::vector<std::string> result = split(str, pattern);
    std::cout << "The result: " << std::endl;
    for( int i = 0; i < result.size(); i++ )
    {
        std::cout << result[i] << std::endl;
    }
    
    std::cin.get();
    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值