p; 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类结束
/////////////////////