当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

几个实用字符串处理函数

    几个实用字符串处理函数


    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;
    }


 

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接