字符串处理

发表于:2007-05-25来源:作者:点击数: 标签:几个实用字符串处理
几个实用字符串处理函数 char *intercept(char *result_str, char const *former_str,int const begin_place_num,char const *begin_str, char const *end_str) /*功能:begin_place0时,取former_str中第begin_place个begin_str开始到end_str结束的字符串,b

几个实用字符串处理函数

char *intercept(char *result_str, char const *former_str,int const begin_place_num,char const *begin_str, char const *end_str)
/*功能:begin_place>0时,取former_str中第begin_place个begin_str开始到end_str结束的字符串,begin_place<0时, 取第begin_place个begin_str左边的字符串,begin_place=0时取到空*/
/*result_str=结果字符串,former_str=源字符串,begin_place_num=第几个(开始字符串),begin_str=开始字符串,end_str结束字符串*/
{
        char *l_current_place,*r_current_place,*swap_str;
        int i;

        if ( (begin_place_num==0) || ((l_current_place=strstr(former_str,begin_str)) == NULL) )  {
                *result_str='';
                return NULL;
        }

        for (i=2;i <= abs(begin_place_num);i++) {
                swap_str = l_current_place;
                if ( (l_current_place=strstr(swap_str+1,begin_str)) == NULL ) {
                        *result_str='';
                        return NULL;
                }
        }

        if (l_current_place == NULL) {
                *result_str='';
                return NULL;
        }

        if( begin_place_num>0 ) {
                r_current_place=strstr(l_current_place+strlen(begin_str),end_str);
                l_current_place=l_current_place+strlen(begin_str);
                if( strlen(begin_str)!=0 && strlen(end_str)!=0 )
                        if (l_current_place == r_current_place) {
                                *result_str++='';
                                return NULL;
                        }
                while ((*result_str++ = *l_current_place++) != 0)
                        if (l_current_place == r_current_place) {
                                *result_str++='';
                                break;
                        }
        }
        else {
                while( (*result_str++ = *former_str++) != 0)
                        if ( former_str == l_current_place ){
                                *result_str++ = '';
                                break;
                        }
        }
        return result_str;
}

char * r_intercept(char *result_str, char const *former_str,int const begin_place_num,char const *begin_str)
/*功能:begin_place>0时,取former_str中倒数第begin_place个begin_str开始到结束的字符串,begin_place<0时, 取倒数第begin_place个begin_str左边的字符串,begin_place=0时取到空*/
/*result_str=结果字符串,former_str=源字符串,begin_place_num=第几个(开始字符串),begin_str=开始字符串*/
{

        char *current_place,*swap_str;
        int i,repeat_left,total=0;
        if ( (begin_place_num==0) || ((current_place=strstr(former_str,begin_str)) == NULL) ) {
                *result_str = '';
                return NULL;
        }
        while (current_place != NULL) {
                swap_str=current_place;
                current_place = strstr(swap_str+1,begin_str);
                total++;
        }
        if ( (repeat_left=total-abs(begin_place_num)+1 ) <= 0 ) {
                *result_str = '';
                return NULL;
        }

        if ( (current_place=strstr(former_str,begin_str)) == NULL) {
                result_str = '';
                return NULL;
        }
        for (i=2;i <= repeat_left;i++) {
                swap_str=current_place;
                if ( (current_place=strstr(swap_str+1,begin_str)) == NULL) {
                        *result_str='';
                        return NULL;
                }
        }

        if (begin_place_num > 0) {
                current_place=current_place+strlen(begin_str);
                while ((*result_str++ = *current_place++) != 0)
                        ;
        }
        else
        {
                while ((*result_str++ = *former_str++) != 0)
                {
                        if (former_str == current_place)
                        {
                                *result_str++ = '';
                                break;
                        }
                }
        }
        return result_str;
}

char *clear_blank(char  *string)
/*清除字符串两端的空格,回车等*/
{
        char *strl,*strr;
        for(strl=string;((*strl==' ')||(*strl == '\t')||(*strl == '\n')||(*strl == '\r') );*strl++);
        strr=string+strlen(string)-1;
        for(;(strr>=string) && ((*strr == ' ')||(*strr == '\n')||(*strr == '\r')||(*strr == '\t') );strr--);
        for(;strl<=strr;string++,strl++)
                *string=*strl;
        *string++='';
        return string;
}

int ifloatcheck(char *string)
/*判断字符串是不是实数,0=是,1=否*/
{
        char strnum[50];
        char *strl,*strr,*str;
        int i=0;
        double numstr;
        str=string;

        for(strl=string;((*strl==' ')||(*strl == '\t')||(*strl == '\n')||(*strl == '\r') );*strl++);
        strr=string+strlen(string)-1;
        for(;(strr>=string) && ((*strr == ' ')||(*strr == '\n')||(*strr == '\r')||(*strr == '\t') );strr--);
        for(;strl<=strr;string++,strl++)
                *string=*strl;
        *string++='';
        string=str;

        while(*string!='')
        {
                if(!isdigit(*string)&&*string!='.'&&*string!='+'&&*string!='-'&&*string!=' ')
                        return 1;
                *string++;
        }
        string=str;

        if(strcmp(string,"+")==0)
                i=1;
        numstr=atof(string);
        sprintf(strnum,"+%lf",numstr);
        if(strstr(strnum,string)==NULL)
                i=1;
        return i;
}

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)