?? stringtool.cpp
字號(hào):
#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 循環(huán)實(shí)現(xiàn)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先結(jié)束,也返回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;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -