⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtool.cpp

📁 一个HTTP协议的封装类
💻 CPP
字号:
#include "StdAfx.h"
#include "StringTool.h"
#include <string.h>
#include <cmath>
#include <cctype>

int StringTool::ERROR=-1;
int StringTool::SUCCESS=1;

int StringTool::Find(const char *pc, char c ,int n)
{
	int m=0;

	for(int i=0;pc[i];i++)
	{
		char ch=pc[i];
		if(ch==c)
		{
			m++;

			if (m==n)
			{
				return i;
			}
		}
	}

	return -1;
}

int StringTool::Find(const char* pSrc,char* pTarget,int lowUp)
{
	for(int i=0;pSrc[i];i++)
	{
		if(BeginWith(pSrc+i,pTarget))
		{
			return i;
		}
	}

	//for 循环实现2
	/*for( int i = 0; pSrc[0] ; i++ )
	{
		if( StringTool::BeginWith( pSrc,pTarget ) )
			return i;
		pSrc++;
	}*/

	return -1;
}

bool StringTool::BeginWith(const  char* pSrc,char* pTarget,int lowUp )
{
	for( int n = 0; pTarget[n]; n++ )
	{
		//如果pSrc先结束,也返回false
		char chSrc=pSrc[n];
		switch(lowUp)
		{
		case -1:
			chSrc=tolower(chSrc);
			break;
		case 1:
			chSrc=toupper(chSrc);
		    break;
		default:
		    break;
		}

		if( pSrc[n] != pTarget[n] )
			return false;
	}
	return true;
}


int StringTool::Left(const char *pSrc, char *pResult, int index)
{
	int srcsize=strlen(pSrc);
	int i=0;
	for(;i<index+1 && i<srcsize;i++)
	{
		if(pSrc[i]=='\0')
			return StringTool::ERROR;
		pResult[i]=pSrc[i];
	}

	pResult[i]='\0';

	return StringTool::SUCCESS;
}

int	 StringTool::Right(const  char* pSrc,char* pResult,int index )
{
	int srcsize=strlen(pSrc);
	if(index<srcsize)
	{
		int n=0;
		for( ; pSrc[index+n] ; n++ )
		{
			pResult[ n ] = pSrc[ index + n ];
		}
		pResult[n]=0;
		if( n )//如果n!=0
			return StringTool::SUCCESS;
	}
	else
	{
		pResult[0]=0;
	}

	return StringTool::ERROR;
}

int	 StringTool::Mid(const  char* pSrc,char* pResult,int index,int len )
{
	int srcsize=strlen(pSrc);
	if(index<srcsize)
	{
		int n=0;
		for( ; pSrc[index+n] && n < len; n++ )
		{
			pResult[ n ] = pSrc[ index + n ];
		}
		pResult[n]=0;
		if( n )
			return StringTool::SUCCESS;
	}
	else
	{
		pResult[0]=0;
	}

	return StringTool::ERROR;
}

float StringTool::Str2Num(const  char*  pSrc )
{
	return ::atof( pSrc );
}


int	 StringTool::Replace(const  char *pSrc,char* pResult,char* rSrc,char* rMent )
{
	int  index = Find( pSrc,rSrc );
	if( index < 0 )
	{
		strcpy( pResult,pSrc );
		return StringTool::ERROR;
	}
	strncpy( pResult,pSrc,index );
	pResult = pResult + index;
	strcpy( pResult,rMent );
	pResult += strlen( rMent );
	pSrc = pSrc + index + strlen( rSrc );
	Replace( pSrc,pResult,rSrc,rMent );
	
	return StringTool::SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -