自定义类_string类
【打印文章】
////////////////////////////////////////////////////////////
// string类源程序
// 时间: 2005-11-2
// 程序员: 黄江斌( blog主页 http://blog.csdn.net/zerodspace
// 开发平台: VC++6.0<<单文件程序>>
////////////////////////////////////////////////////////////
#include "iostream.h"
////////////////////////////////////////////////////////////
// string类声明部分
// 时间: 2005-11-2
// 程序员: 黄江斌
////////////////////////////////////////////////////////////
#include "string.h"
#define TRIM_LEFT 1
#define TRIM_RIGHT 2
#define TRIM_BOTH 0
class string
{
public:
string();
string( const char *src );
string( string &src );
~string();
//操作符重载
void operator = ( const char *src );
//char * operator + ( string &add2 );
string operator + ( string &add2 );
operator char *();
//成员函数
const char *ToString();
char GetAt( int index );
int Len();
int FindFirstSub( string substr , int start );
bool InStr( string substr );
string Trim( int part );
string Left( int sublen );
string Right( int sublen );
string Mid( int start , int sublen );
void Replace( string strNeedReplaced , string strReplace );
private:
char *str;
int len;
};
////////////////////////////////////////////////////////////
// string类实现部分
// 时间: 2005-11-2
// 程序员: 黄江斌
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// string在构造、析构函数
// string()
// string( const char *src )
// string( string &src )
// ~string()
string::string()
{
len = 0;
str = new char(len+1);
str[0] = 0;
}
string::string( const char *src )
{
len = strlen(src);
str = new char(len+1);
strcpy( str , src );
}
string::string( string &src )
{
len = src.Len();
str = new char(len+1);
strcpy( str , (char*)src );
}
string::~string()
{
}
////////////////////////////////////////////////////////////
// string类操作符重载
// string operator = ( const char *src )
// string operator + ( string &add2 )
// operator char *()
void string::operator = ( const char *src )
{
len = strlen(src);
str = new char(len+1);
strcpy( str , src );
}
string string::operator + ( string &add2 )
{
int len2 = add2.Len();
int len0 = len + len2;
char *str0 = new char(len0+1);
strcpy( str0 , str );
strcat( str0 , (char*)add2 );
string sum(str0);
return sum;
}
string::operator char *()
{
return str;
}
////////////////////////////////////////////////////////////
// string类功能函数
// const char *ToString()
// char GetAt( int index )
// int Len()
// int FindFirstSub( string substr , int start = 0 )
// bool InStr( string substr )
// string Trim( int part )
// string Left()
// string Right()
// string Mid()
// void Replace( string strNeedReplaced , string strReplace )
const char *string::ToString()
{
return str;
}
//
//得到内存编号为index的字符
char string::GetAt( int index )
{
return str[index];
}
//
//得到字符串的长度
int string::Len()
{
return len;
}
//
//得到start后(包括start)子串substr的内存编号
int string::FindFirstSub( string substr , int start = 0 )
{
if( start > len )
return -1;
for( int i = start ; i <= len - substr.Len() ; i++ )
{
if( str[i] == substr.GetAt(0) )
{
bool flag = true;
int j;
for( j = 0 ; flag && j < substr.Len() && (i + j < len) ; j++ )
{
if( str[i+j] != substr.GetAt(j) )
flag = false;
}
if( j >= substr.Len() && flag )
{
return i;
}
}
}
return -1;
}
//
//判断字符串substr是否字符串的子串
bool string::InStr( string substr )
{
if( FindFirstSub( substr ) < 0 )
return false;
else
return true;
}
//
//去前导、后导空格
string string::Trim( int part = TRIM_BOTH )
{
string s = *this;
char *temp = new char(Len()+1);
int i , j , k;
if( part == TRIM_LEFT || part == TRIM_BOTH )
{
j = 0;
while( s.GetAt(j) == '' '' && j < s.Len() )
j++;
k = s.Len();
i = 0;
while( j <= k )
temp[i++] = s.GetAt(j++);
s = (string)temp;
}
if( part == TRIM_RIGHT || part == TRIM_BOTH )
{
j = s.Len() - 1;
while( s.GetAt(j) == '' '' )
j--;
for( i = 0 ; i <= j ; i++ )
temp[i] = s.GetAt(i);
temp[i] = 0;
s = (string)temp;
}
return s;
}
//
//字符串左部长度为sublen的子串
string string::Left( int sublen )
{
if( sublen > len )
sublen = len;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//字符串右部长度为sublen的子串
string string::Right( int sublen )
{
if( sublen > len )
sublen = len;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[len-sublen+i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//字符串中间从start开始长度为sublen的子串
string string::Mid( int start , int sublen )
{
if( start > len || sublen <= 0 )
{
string sub("");
return sub;
}
if( start + sublen > len )
sublen = len - start;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[start+i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//将字符串中所有形如strNeedReplaced的子串替换为strReplace
void string::Replace( string strNeedReplaced , string strReplace )
{
string temp;
int start = 0;
int index = FindFirstSub( strNeedReplaced , start );
while( index >= 0 )
{
temp = temp + Mid( start , index - start ) + strReplace;
start = index + strNeedReplaced.Len();
index = FindFirstSub( strNeedReplaced , start );
}
temp = temp + Right( len - start );
len = temp.Len();
str = new char(len+1);
strcpy( str , (char*)temp );
}
// string类结束
////////////////////////////////////////////////////////////
void main()
{
string str = " a b c ";
cout<<str<<"|"<<endl;
while( str.InStr( " " ) )
str.Replace( " " , " " );
cout<<str.Trim()<<"|"<<endl;
}
// string类源程序
// 时间: 2005-11-2
// 程序员: 黄江斌( blog主页 http://blog.csdn.net/zerodspace
// 开发平台: VC++6.0<<单文件程序>>
////////////////////////////////////////////////////////////
#include "iostream.h"
////////////////////////////////////////////////////////////
// string类声明部分
// 时间: 2005-11-2
// 程序员: 黄江斌
////////////////////////////////////////////////////////////
#include "string.h"
#define TRIM_LEFT 1
#define TRIM_RIGHT 2
#define TRIM_BOTH 0
class string
{
public:
string();
string( const char *src );
string( string &src );
~string();
//操作符重载
void operator = ( const char *src );
//char * operator + ( string &add2 );
string operator + ( string &add2 );
operator char *();
//成员函数
const char *ToString();
char GetAt( int index );
int Len();
int FindFirstSub( string substr , int start );
bool InStr( string substr );
string Trim( int part );
string Left( int sublen );
string Right( int sublen );
string Mid( int start , int sublen );
void Replace( string strNeedReplaced , string strReplace );
private:
char *str;
int len;
};
////////////////////////////////////////////////////////////
// string类实现部分
// 时间: 2005-11-2
// 程序员: 黄江斌
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// string在构造、析构函数
// string()
// string( const char *src )
// string( string &src )
// ~string()
string::string()
{
len = 0;
str = new char(len+1);
str[0] = 0;
}
string::string( const char *src )
{
len = strlen(src);
str = new char(len+1);
strcpy( str , src );
}
string::string( string &src )
{
len = src.Len();
str = new char(len+1);
strcpy( str , (char*)src );
}
string::~string()
{
}
////////////////////////////////////////////////////////////
// string类操作符重载
// string operator = ( const char *src )
// string operator + ( string &add2 )
// operator char *()
void string::operator = ( const char *src )
{
len = strlen(src);
str = new char(len+1);
strcpy( str , src );
}
string string::operator + ( string &add2 )
{
int len2 = add2.Len();
int len0 = len + len2;
char *str0 = new char(len0+1);
strcpy( str0 , str );
strcat( str0 , (char*)add2 );
string sum(str0);
return sum;
}
string::operator char *()
{
return str;
}
////////////////////////////////////////////////////////////
// string类功能函数
// const char *ToString()
// char GetAt( int index )
// int Len()
// int FindFirstSub( string substr , int start = 0 )
// bool InStr( string substr )
// string Trim( int part )
// string Left()
// string Right()
// string Mid()
// void Replace( string strNeedReplaced , string strReplace )
const char *string::ToString()
{
return str;
}
//
//得到内存编号为index的字符
char string::GetAt( int index )
{
return str[index];
}
//
//得到字符串的长度
int string::Len()
{
return len;
}
//
//得到start后(包括start)子串substr的内存编号
int string::FindFirstSub( string substr , int start = 0 )
{
if( start > len )
return -1;
for( int i = start ; i <= len - substr.Len() ; i++ )
{
if( str[i] == substr.GetAt(0) )
{
bool flag = true;
int j;
for( j = 0 ; flag && j < substr.Len() && (i + j < len) ; j++ )
{
if( str[i+j] != substr.GetAt(j) )
flag = false;
}
if( j >= substr.Len() && flag )
{
return i;
}
}
}
return -1;
}
//
//判断字符串substr是否字符串的子串
bool string::InStr( string substr )
{
if( FindFirstSub( substr ) < 0 )
return false;
else
return true;
}
//
//去前导、后导空格
string string::Trim( int part = TRIM_BOTH )
{
string s = *this;
char *temp = new char(Len()+1);
int i , j , k;
if( part == TRIM_LEFT || part == TRIM_BOTH )
{
j = 0;
while( s.GetAt(j) == '' '' && j < s.Len() )
j++;
k = s.Len();
i = 0;
while( j <= k )
temp[i++] = s.GetAt(j++);
s = (string)temp;
}
if( part == TRIM_RIGHT || part == TRIM_BOTH )
{
j = s.Len() - 1;
while( s.GetAt(j) == '' '' )
j--;
for( i = 0 ; i <= j ; i++ )
temp[i] = s.GetAt(i);
temp[i] = 0;
s = (string)temp;
}
return s;
}
//
//字符串左部长度为sublen的子串
string string::Left( int sublen )
{
if( sublen > len )
sublen = len;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//字符串右部长度为sublen的子串
string string::Right( int sublen )
{
if( sublen > len )
sublen = len;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[len-sublen+i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//字符串中间从start开始长度为sublen的子串
string string::Mid( int start , int sublen )
{
if( start > len || sublen <= 0 )
{
string sub("");
return sub;
}
if( start + sublen > len )
sublen = len - start;
char *substr = new char(sublen+1);
for( int i = 0 ; i < sublen ; i++ )
substr[i] = str[start+i];
substr[sublen] = 0;
string sub(substr);
return sub;
}
//
//将字符串中所有形如strNeedReplaced的子串替换为strReplace
void string::Replace( string strNeedReplaced , string strReplace )
{
string temp;
int start = 0;
int index = FindFirstSub( strNeedReplaced , start );
while( index >= 0 )
{
temp = temp + Mid( start , index - start ) + strReplace;
start = index + strNeedReplaced.Len();
index = FindFirstSub( strNeedReplaced , start );
}
temp = temp + Right( len - start );
len = temp.Len();
str = new char(len+1);
strcpy( str , (char*)temp );
}
// string类结束
////////////////////////////////////////////////////////////
void main()
{
string str = " a b c ";
cout<<str<<"|"<<endl;
while( str.InStr( " " ) )
str.Replace( " " , " " );
cout<<str.Trim()<<"|"<<endl;
}
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】