文字列比較

// s1 と s2 を func で処理し比較した結果を戻す
const int xcompare(const std::string& s1, const std::string& s2, int (*func)(int num)=toupper)
{	assert(s1.size()==s2.size()); // s1 と s2 の長さは同じなはず
	int result=0;
	std::string::const_iterator it1(s1.begin());
	std::string::const_iterator it2(s2.begin());
	const std::string::const_iterator end1(s1.end());
	const std::string::const_iterator end2(s2.end());
	for( ; it1!=end1 && it2!=end2 && result==0; ++it1,++it2 )
	{
		result = ((*func)(*it1)-(*func)(*it2));
	}
	return result;
}
// s1 と s2 を stricmp 比較した結果を戻す (要 #include )
const int icompare(const std::string& s1, const std::string& s2)
{	assert(s1.size()==s2.size()); // s1 と s2 の長さは同じなはず
	return stricmp(s1.c_str(),s2.c_str());
}

文字列分割

// src を delim 毎に分割して dst に格納する
void split(const std::string& src, std::vector& dst, const std::string& delim, const bool getNull=false)
{
	std::string::size_type lastPos = 0;
	std::string::size_type foundPos = !std::string::npos;
	std::string::size_type delimLen = delim.size();
	while( (foundPos=src.find(delim,lastPos))!=std::string::npos )
	{
		if( ! getNull && foundPos==lastPos )
		{
			lastPos += delimLen;
			continue;
		}
		dst.push_back( src.substr(lastPos,foundPos-lastPos) );
		lastPos = foundPos+delimLen;
	}
	// 最終検索位置が分割対象の最後尾まで達していない場合
	if( lastPos < src.size()-1 )
	{   // 最終検索位置から後ろをベクターに格納
		dst.push_back( src.substr(lastPos) );
	}
}
// src の先頭から delim を探し, 見つかった位置で left と right に分ける
// 見つからない場合は left, right 共に空となる
void split(const std::string& src, const std::string& delim, std::string& left, std::string& right)
{
	size_t pos = src.find(delim);
	if(pos!=std::string::npos)
	{
		left.swap( std::string(src,0,pos) );
		right.swap( std::string(src,pos+delim.size()) );
	}
}

文字列置換

// src_dst の先頭から oldStr を探して newStr に置き換える
void replace(std::string& src_dst, const std::string& oldStr, const std::string& newStr)
{
	std::string::size_type foundPos = src_dst.find(oldStr);
	const std::string::size_type oldSize = oldStr.size();
	const std::string::size_type newSize = newStr.size();
	while(foundPos!=std::string::npos)
	{
		src_dst.replace(foundPos,oldSize,newStr);
		foundPos = src_dst.find(oldStr,foundPos+newSize);
	}
}
// src_dst の後尾から oldStr を探して newStr に置き換える
void replace_r(std::string& src_dst, const std::string& oldStr, const std::string& newStr)
{
	std::string::size_type foundPos = src_dst.rfind(oldStr);
	const std::string::size_type oldSize = oldStr.size();
	const std::string::size_type newSize = newStr.size();
	while(foundPos!=std::string::npos)
	{
		src_dst.replace(foundPos,oldSize,newStr);
		foundPos = src_dst.rfind(oldStr,foundPos-newSize);
	}
}

文字列削除

// src_dst の先頭が target で始まる場合に target 部分を削除する (compare版)
void trim_first_comp(std::string& src_dst, const std::string& target, const bool recursive)
{
	size_t pos = 0;
	const size_t tsz = target.size();
	if( recursive )
	{
		for( ; src_dst.compare(pos,tsz,target)==0; pos+=tsz ){}
	}
	else
	{
		if(src_dst.compare(pos,tsz,target)==0){ pos+=tsz; }
	}
	src_dst.erase(0,pos);
}
// src_dst の先頭が target で始まる場合に target 部分を削除する (find版)
void trim_first_find(std::string& src_dst, const std::string& target, const bool recursive)
{
	size_t pos = 0;
	const size_t tsz = target.size();
	if( recursive )
	{
		for( ; src_dst.find(target,pos)==pos; pos+=tsz ){};
	}
	else
	{
		if(src_dst.find(target,pos)==pos){ pos+=tsz; }
	}
	src_dst.erase(0,pos);
}
// src_dst の後尾から target で終わる場合に target 部分を削除する (compare版)
void trim_last_comp(std::string& src_dst, const std::string& target, const bool recursive)
{
	const size_t tsz = target.size();
	size_t pos = src_dst.size();
	if( recursive )
	{
		for( ; src_dst.compare(pos-tsz,tsz,target)==0; pos-=tsz ){}
	}
	else
	{
		if(src_dst.compare(pos-tsz,tsz,target)==0){ pos-=tsz; }
	}
	src_dst.erase(pos);
}
// src_dst の後尾から target で終わる場合に target 部分を削除する (rfind版)
void trim_last_rfind(std::string& src_dst, const std::string& target, const bool recursive)
{
	size_t ssz = src_dst.size();
	const size_t tsz = target.size();
	size_t pos = ssz;
	if( recursive )
	{
		for(; src_dst.rfind(target,pos-1)==pos-tsz; pos-=tsz){}
	}
	else
	{
		if(src_dst.rfind(target,pos-1)==ssz-tsz){ pos-=tsz; }
	}
	src_dst.erase(pos);
}
// src_dst の両端が target の場合に target 部分を削除する
void trim_side_char(std::string& src_dst, const char target, const bool recursive, const bool sync)
{
	size_t posf=0;
	size_t posl=src_dst.size();
	if(sync)
	{
		if(recursive)
		{
			while( src_dst.at(posf)==target && src_dst.at(posl-1)==target )
			{
				++posf;
				--posl;
			}
		}
		else
		{
			if(src_dst.at(posf)==target && src_dst.at(posl-1)==target)
			{
				++posf;
				--posl;
			}
		}
	}
	else
	{
		if(recursive)
		{
			for( ; src_dst.at(posf)==target; ++posf){}
			for( ; src_dst.at(posl-1)==target; --posl){}
		}
		else
		{
			if(src_dst.at(posf)==target){ ++posf; }
			if(src_dst.at(posl-1)==target){ --posl; }
		}
	}
	src_dst.erase(posl);
	src_dst.erase(0,posf);
}