📄 ostring.cpp
字号:
static const char* sccsid = "@(#)ostring.cpp v1.0.0 2000-08-23";
/* Copyright(C) 1999, 2000 by JiangSu Bell Software CO.,LTD. */
/*
Name: cursor.cpp Version: 1.0.0
Created by Zhuliang Date: 2000-08-23
Comment: Our group defining all base-class sets
Modified:
1) 2000-08-23 Zhuliang - Create;
*/
#include "common.h"
/* 构造字符串函数 */
OString :: OString()
{
ntemplen = 1;
pctemp = new char [ 1 ];
pctemp[ 0 ] = 0;
}
/* 构造字符串函数, newlen为申请长度 */
OString :: OString( long newlen )
{
ntemplen = newlen + 1;
pctemp = new char [ ntemplen ];
pctemp[ 0 ] = 0;
}
/* 构造字符串函数,并赋值char* csS */
OString :: OString( const char *csS )
{
ntemplen = strlen( csS ) + 1;
pctemp = new char [ ntemplen ];
strcpy( pctemp, csS );
}
/* 构造字符串函数,并赋值OString &csOString */
OString :: OString( const OString &csOString )
{
ntemplen = csOString.GetLen() + 1;
pctemp = new char [ ntemplen ];
strcpy( pctemp, csOString );
}
/* 析构函数 */
OString :: ~OString()
{
delete pctemp;
}
/* 重载操作符= 赋值char* csS */
OString& OString :: operator = ( const char *csS )
{
ntemplen = strlen( csS ) + 1;
delete pctemp;
pctemp = new char [ ntemplen ];
strcpy( pctemp, csS );
return *this;
}
/* 重载操作符= 赋值OString &csString */
OString& OString :: operator = ( OString &csString )
{
ntemplen = csString.GetLen( ) + 1;
delete pctemp;
pctemp = new char [ ntemplen ];
strcpy( pctemp, csString );
return *this;
}
/* 取OString的实际长度 */
long OString :: GetLen( ) const
{
return ntemplen - 1;
}
OString glbString; //全局变量,为以下三个操作应用
/* 将OString与char*相加,返回另一个OString */
OString& operator + ( const OString &csOstr, const char *csS )
{
char *pcS = new char [ csOstr.GetLen() + strlen( csS ) + 1 ];
strcpy( pcS, csOstr );
strcat( pcS, csS );
glbString = pcS;
delete pcS;
return glbString;
}
/* 将OString1与OString2相加,返回另一个OString */
OString& operator + ( const OString &csOstr1, const OString &csOstr2 )
{
char *pcS = new char [ csOstr1.GetLen() + csOstr2.GetLen() + 1 ];
strcpy( pcS, csOstr1 );
strcat( pcS, csOstr2 );
glbString = pcS;
delete pcS;
return glbString;
}
/* 从一个OString的第nBegin位开始(最小为0),截取nLen位,返回另一个OString */
OString& OString :: SubString( long nBegin, long nLen )
{
if ( ( nBegin<0 )||( nLen<=0 ) )
glbString = "";
else
{
if ( (nBegin+nLen)>(ntemplen-1) )
nLen = ntemplen - nBegin - 1;
char *pcS = new char [ ntemplen ];
strcpy( pcS, pctemp + nBegin );
pcS[ nLen ] = 0;
glbString = pcS;
delete pcS;
}
return glbString;
}
/* 去掉一个OString的nBegin位,返回另一个OString */
OString& OString :: EraseOne( long nBegin )
{
if ( nBegin < (ntemplen-1) )
{
long i;
for ( i = nBegin; i < (ntemplen-2); i ++ )
pctemp[ i ] = pctemp[ i + 1 ];
pctemp[ i ] = 0;
ntemplen = ntemplen - 1;
}
return *this;
}
/* 重载+= 自己累加另一csS */
OString& OString :: operator += ( const char* csS )
{
char *pcS = new char [ ntemplen ];
strcpy( pcS, pctemp );
ntemplen = ntemplen + strlen( csS );
delete pctemp;
pctemp = new char [ ntemplen ];
strcpy( pctemp, pcS );
strcat( pctemp, csS );
delete pcS;
return *this;
}
/* 重载+= 自己累加另一OString */
OString& OString :: operator += ( const OString &csString )
{
char *pcS = new char [ ntemplen ];
strcpy( pcS, pctemp );
ntemplen = ntemplen + csString.GetLen( );
delete pctemp;
pctemp = new char [ ntemplen ];
strcpy( pctemp, pcS );
strcat( pctemp, csString );
delete pcS;
return *this;
}
/* OString赋空 */
void OString :: Empty( )
{
ntemplen = 1;
delete pctemp;
pctemp = new char [ 1 ];
pctemp[ 0 ] = 0;
}
/* 删除左边空格 */
OString& OString :: Ltrim( )
{
long i ;
char *pcS = new char[ ntemplen ];
while ( pctemp[0] == ' ' )
{
for ( i = 0 ; i < ntemplen; i++ )
{
pctemp[ i ] = pctemp[ i + 1 ];
}
ntemplen --;
}
strcpy( pcS, pctemp );
delete pctemp;
pctemp = new char[ ntemplen ];
strcpy( pctemp, pcS );
delete pcS;
return *this;
}
/* 删除右边空格 */
OString& OString :: Rtrim( )
{
long i = ntemplen - 1;
while ( pctemp[ i - 1 ] == ' ' )
i --;
ntemplen = i + 1;
pctemp[ ntemplen - 1 ] = 0;
char *pcS = new char[ ntemplen ];
strcpy( pcS, pctemp );
char *pctemp = new char[ ntemplen ];
strcpy( pctemp, pcS );
delete pcS;
return *this;
}
/* 删除左右边空格,但不删除中间空格 */
OString& OString :: Trim( )
{
long i = ntemplen - 1;
while ( pctemp[ i - 1] == ' ' )
i --;
ntemplen = i + 1;
pctemp[ ntemplen - 1 ] = 0;
char *pcS = new char[ ntemplen ];
while ( pctemp[0] == ' ' )
{
for ( i = 0 ; i < ntemplen; i++ )
{
pctemp[ i ] = pctemp[ i + 1 ];
}
ntemplen --;
}
strcpy( pcS, pctemp );
delete pctemp;
pctemp = new char[ ntemplen ];
strcpy( pctemp, pcS );
delete pcS;
return *this;
}
/* 重载[] 取字符串的第nAt位字符 */
char OString :: operator () ( long nAt )
{
if ( nAt >= ntemplen )
return 0;
else
return pctemp[ nAt ];
}
char glbChar = 0;
char& OString :: operator [] ( long nAt )
{
if ( ntemplen > ( nAt + 1 ) )
return pctemp[ nAt ];
else
{
glbChar = 0;
return glbChar;
}
}
/* 找出字符串中含有csS的位置,第一位为0,找不到返回-1 */
long OString :: Find( const char* csS )
{
long nLen = strlen( csS );
long i = 0, j = 0;
char *pcS = new char [ ntemplen ];
while ( i < (ntemplen - nLen) && j == 0 )
{
strcpy( pcS, pctemp + i );
pcS[ nLen ] = 0;
if ( !strcmp( pcS, csS ) )
j = 1;
else
i ++;
}
if ( j == 0 )
i = -1;
delete pcS;
return i;
}
/* 找出字符串中含有csOstr的位置,第一位为0,找不到返回-1 */
long OString :: Find( OString &csOstr )
{
long nLen = csOstr.GetLen( );
long i = 0, j = 0;
char *pcS = new char [ ntemplen ];
while ( i < (ntemplen - nLen) && j == 0 )
{
strcpy( pcS, pctemp + i );
pcS[ nLen ] = 0;
if ( !strcmp( pcS, csOstr ) )
j = 1;
else
i ++;
}
if ( j == 0 )
i = -1;
delete pcS;
return i;
}
/* 字符串比较,返回值同 strcmp */
short OString :: Compare( const char* csS )
{
short i = strcmp( pctemp, csS );
return i;
}
/* 字符串比较,返回值同 strcmp */
short OString :: Compare( OString &csOstr )
{
short i = strcmp( pctemp, csOstr );
return i;
}
/* 判字符串是否为空,若空返回1,否则为0 */
bool OString :: IsNull( )
{
if ( ntemplen == 1 )
return true;
else
return false;
}
/* 所有字符串改为大写 */
OString& OString :: Upper( )
{
long i;
for ( i = 0; i < ntemplen; i++ )
pctemp[ i ] = toupper( pctemp[ i ] );
return *this;
}
/* 所有字符串改为小写 */
OString& OString :: Lower( )
{
long i;
for ( i = 0; i < ntemplen; i++ )
pctemp[ i ] = tolower( pctemp[ i ] );
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -