游戏开发常用字符串函数整理【C++版】

游戏开发过程中常用的,函数参考python;

觉得有些细节不够优化的,可以自行处理。


/************************************************************************/
/*      UT_BaseStringUtility.h                                          */
/************************************************************************/
#pragma once

#include 
   
   
    
    
#include 
    
    
     
     


typedef    std::string                        StdString;
typedef    std::vector
     
     
      
                 StdStringVector;


namespace UT_String
{
	bool                    startsWith(const StdString& str, const StdString& strSub, bool caseSensitive = true );
	bool                    endsWith(const StdString& str, const StdString& strSub, bool caseSensitive = true );
	bool                    contains(const StdString& str, const StdString& strSub, bool caseSensitive = true );
	StdStringVector         split( const StdString& rkString , char cSplit );
	StdStringVector         split( const StdString& rkString , const StdString& sSplit );
	StdStringVector         partition( const StdString& rkString , const StdString& sSplit );
	StdStringVector         rpartition( const StdString& rkString , const StdString& sSplit );
	StdString               join( const StdString& sJoin , const StdStringVector& sSeqs );
	int                     compare(const StdString& rklv,const StdString& rkrv,bool bCasesensitive = false);
	void                    trim(StdString& str, bool left = true , bool right = true, const char * my_replace_delims = NULL, const char * my_external_delims = NULL );
	void                    remove(StdString& str, const StdString& strSub);
	StdString               replace(const StdString& kMainString, const StdString& kSrc, const StdString& kDst);
	StdString               left(const StdString& str, StdString::size_type nCount);
	StdString               right(const StdString& str, StdString::size_type nCount);
	StdString               mid(const StdString& str, StdString::size_type nPos, StdString::size_type nCount = StdString::npos);
	StdString::size_type    indexOf(const StdString& str, const StdString& strSub);
	int                     format(StdString& str, const char *szFormat, ...);

	void                    toLower(StdString& str);
	void                    toUpper(StdString& str);
	StdString               lower(const StdString& str);
	StdString               upper(const StdString& str);

	int                     toInt(const StdString& str);
};



/************************************************************************/
/*     UT_BaseStringUtility.cpp                                         */
/************************************************************************/
#include "UT_BaseStringUtility.h"
#include 
      
      
       
       

namespace	UT_String
{
#define JZChar          char
#define JZString        StdString
#define JZStringVector  StdStringVector
	//-------------------------------------------------------------------------
	bool startsWith(const JZString& str, const JZString& strSub, bool caseSensitive )
	{
		if (strSub.empty())				// l_sub == 0
			return false;

		size_t l = str.length();
		size_t l_sub = strSub.length();	// l_sub > 0
		if (l_sub > l)
			return false;

		if (caseSensitive)
		{
			int x = str.compare(0, l_sub, strSub);
			return x == 0;
		}

		for (size_t i = 0; i < l_sub; i++)	// not caseSensitive
		{
			int c = str[i];
			int d = strSub[i];

			c = tolower(c);
			d = tolower(d);

			if (c != d)
			{
				return false;
			}
		}

		return true;
	}
	//-------------------------------------------------------------------------
	bool endsWith(const JZString& str, const JZString& strSub, bool caseSensitive )
	{
		if (strSub.empty())				// l_sub == 0
			return false;

		size_t l = str.length();
		size_t l_sub = strSub.length();	// l_sub > 0
		if (l < l_sub)
			return false;

		int diff = (int)(l - l_sub);	// l >= l_sub
		if (caseSensitive)
		{
			int x = str.compare(diff, l_sub, strSub);
			return x == 0;
		}

		for (size_t i = 0; i < l_sub; i++)	// not caseSensitive
		{
			int c = str[i + diff];
			int d = strSub[i];

			c = tolower(c);
			d = tolower(d);

			if (c != d)
			{
				return false;
			}
		}

		return true;
	}
	//-------------------------------------------------------------------------
	bool contains(const JZString& str, const JZString& strSub, bool bCaseSensitive )
	{
		if (bCaseSensitive)
			return str.find(strSub) != str.npos;

		JZString kLv = str;
		JZString kRv = strSub;

		std::transform(str.begin(),str.end(),kLv.begin(),tolower);
		std::transform(strSub.begin(),strSub.end(),kRv.begin(),tolower);

		return kLv.find(kRv) != kLv.npos;
	}
	//-------------------------------------------------------------------------
	JZStringVector split( const JZString& rkString , char cSplit )
	{
		JZStringVector kResult;
		kResult.reserve(1);

		JZString kFormatPath = rkString;
		JZString::size_type kPos = kFormatPath.find_first_of(cSplit);
		if ( kPos == JZString::npos )
		{
			if (!kFormatPath.empty())
				kResult.push_back( kFormatPath);
			return kResult;
		}

		JZString kSubPath = kFormatPath;

		while(kPos != JZString::npos && kFormatPath.length() > 0)
		{
			kSubPath = kFormatPath.substr(0,kPos);
			kFormatPath = kFormatPath.substr(kPos + 1);

			kResult.push_back(kSubPath);

			kPos = kFormatPath.find_first_of(cSplit);
		}
		if ( kPos == JZString::npos && kFormatPath.length() > 0 )
		{
			kResult.push_back( kFormatPath );
		}
		return kResult;
	}
	//-------------------------------------------------------------------------
	JZStringVector split( const JZString& rkString , const JZString& sSplit )
	{
		JZStringVector kResult;
		kResult.reserve(1);

		JZString kFormatPath = rkString;
		JZString::size_type kPos = kFormatPath.find(sSplit);
		if ( kPos == JZString::npos )
		{
			if (!kFormatPath.empty())
				kResult.push_back( kFormatPath);
			return kResult;
		}

		JZString kSubPath = kFormatPath;

		while(kPos != JZString::npos && kFormatPath.length() > 0)
		{
			kSubPath = kFormatPath.substr(0,kPos);
			kFormatPath = kFormatPath.substr(kPos + sSplit.length());

			kResult.push_back(kSubPath);

			kPos = kFormatPath.find(sSplit);
		}
		if ( kPos == JZString::npos && kFormatPath.length() > 0 )
		{
			kResult.push_back( kFormatPath );
		}
		return kResult;
	}
	//-------------------------------------------------------------------------
	JZStringVector partition( const JZString& rkString , const JZString& sSplit )
	{
		JZStringVector kResult;
		kResult.reserve(3);

		JZString kFormatPath = rkString;
		JZString::size_type kPos = kFormatPath.find_first_of(sSplit);
		if ( kPos == JZString::npos )
		{
			kResult.push_back( kFormatPath);
			kResult.push_back( JZString());
			kResult.push_back( JZString());

			return kResult;
		}
		else
		{
			JZString p0 = kFormatPath.substr(0,kPos);
			JZString p2 = kFormatPath.substr(kPos + sSplit.length());

			kResult.push_back(p0);
			kResult.push_back(sSplit);
			kResult.push_back(p2);
		}

		return kResult;
	}
	//-------------------------------------------------------------------------
	JZStringVector rpartition( const JZString& rkString , const JZString& sSplit )
	{
		JZStringVector kResult;
		kResult.reserve(3);

		JZString kFormatPath = rkString;
		JZString::size_type kPos = kFormatPath.find_last_of(sSplit);
		if ( kPos == JZString::npos )
		{
			kResult.push_back( JZString());
			kResult.push_back( JZString());
			kResult.push_back( kFormatPath);

			return kResult;
		}
		else
		{
			JZString p0 = kFormatPath.substr(0,kPos);
			JZString p2 = kFormatPath.substr(kPos + sSplit.length());

			kResult.push_back(p0);
			kResult.push_back(sSplit);
			kResult.push_back(p2);
		}

		return kResult;
	}
	//-------------------------------------------------------------------------
	JZString join( const JZString& sJoin , const JZStringVector& sSeqs )
	{
		JZString	ret;
		for (JZStringVector::const_iterator it = sSeqs.begin(); it != sSeqs.end(); ++it)
		{
			ret.append(*it);
			ret.append(sJoin);
		}
		if (sSeqs.size() > 0)
			ret.erase(ret.length() - sJoin.length());

		return ret;
	}
	//-------------------------------------------------------------------------
	int compare(const JZString& rklv, const JZString& rkrv,bool bCaseSensitive)
	{
		if(bCaseSensitive)
			return  rklv.compare(rkrv.c_str());

		JZString kLv = rklv;
		JZString kRv = rkrv;

		std::transform(rklv.begin(),rklv.end(),kLv.begin(),tolower);
		std::transform(rkrv.begin(),rkrv.end(),kRv.begin(),tolower);

		return  kLv.compare(kRv.c_str());
	}
	//-------------------------------------------------------------------------
	void trim( JZString& str, bool left, bool right, const JZChar * my_replace_delims/* = NULL*/, const JZChar * my_external_delims/* = NULL*/ )
	{
		static const JZString s_default_delims = " \t\r\n";
		const JZString delims = my_replace_delims ? my_replace_delims : (my_external_delims ? s_default_delims + my_external_delims :s_default_delims);
		if(right)
			str.erase(str.find_last_not_of(delims)+1); // trim right
		if(left)
			str.erase(0, str.find_first_not_of(delims)); // trim left
	}
	//-------------------------------------------------------------------------
	void remove( JZString& str, const JZString& strSub )
	{
		JZString::size_type nCount = strSub.length();
		JZString::size_type nPos = str.find(strSub);
		while (nPos != JZString::npos)
		{
			str.erase(nPos, nCount);
			nPos = str.find(strSub, nPos+1);
		}
	}
	//-------------------------------------------------------------------------
	JZString replace(const JZString& kMainString, const JZString& kSrc, const JZString& kDst)
	{
		JZString rkResult = kMainString;
		JZString::size_type pos = 0;
		JZString::size_type srcLen = kSrc.size();
		JZString::size_type dstLen = kDst.size();
		while ( (pos = rkResult.find(kSrc, pos)) != JZString::npos )
		{
			rkResult.replace(pos, srcLen, kDst);
			pos += dstLen;
		}

		return rkResult;
	}
	//-------------------------------------------------------------------------
	JZString		left(const JZString& str, JZString::size_type nCount)
	{
		if( nCount >= str.length() )
			return JZString(str);

		return str.substr(0, nCount);
	}
	//-------------------------------------------------------------------------
	JZString		right(const JZString& str, JZString::size_type nCount)
	{
		if( nCount >= str.length() )
			return JZString(str);

		return str.substr(str.length() - nCount);
	}
	//-------------------------------------------------------------------------
	JZString		mid(const JZString& str, JZString::size_type nPos, JZString::size_type nCount)
	{
		if( nPos >= str.length() )
			return JZString(str);

		return str.substr(nPos, nCount);
	}
	//-------------------------------------------------------------------------
	JZString::size_type	indexOf(const JZString& str, const JZString& strSub)
	{
		return str.find(strSub);
	}
	//-------------------------------------------------------------------------
	int 	format(JZString& str, const JZChar *szFormat, ...)
	{
		int len = 0;
		JZChar tempStr[1024];

		va_list ArgPtr;	
		va_start( ArgPtr, szFormat );
		len += vsprintf_s(tempStr, 1023, szFormat, ArgPtr);
		va_end( ArgPtr );

		str = tempStr;

		return len;
	}
	void		toLower(JZString& str)
	{
		std::transform(str.begin(),str.end(),str.begin(),tolower);
	}
	void		toUpper(JZString& str)
	{
		std::transform(str.begin(),str.end(),str.begin(),toupper);
	}
	JZString		lower(const JZString& str)
	{
		JZString kResult = str;
		std::transform(str.begin(),str.end(),kResult.begin(),tolower);
		return	kResult;
	}
	JZString		upper(const JZString& str)
	{
		JZString kResult = str;
		std::transform(str.begin(),str.end(),kResult.begin(),toupper);
		return	kResult;
	}
	//-------------------------------------------------------------------------
	int				toInt(const JZString& str)
	{
		return atol(str.c_str());
	}

#undef JZChar
#undef JZString
#undef JZStringVector
}	//namspace

      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值