#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
C语言实现类似java的字符串分割方式
考虑到字符串内存释放问题,进行统一释放内存
风的影子 编写
2019-11-08 
*/

typedef struct TEXT_NODE{
    char *node;
    struct TEXT_NODE *next;
} TEXT_NODE;

typedef struct TEXT_SPLIT {
    char *text; //原字符串
    char *regex; //分割的字符串
    TEXT_NODE *node; //分割后的字符串链表
} TEXT_SPLIT;

/*
 *创建一个字符串分割集合,已regex进行分割
 */
TEXT_SPLIT *create_split(char *text, char *regex){
    TEXT_SPLIT *text_split = (TEXT_SPLIT*)malloc(sizeof(TEXT_SPLIT));
    printf("111");
    text_split->text = malloc(strlen(text)+1);
    strcpy(text_split->text, text);
    text_split->regex = malloc(strlen(regex)+1);
    strcpy(text_split->regex, regex);
    text_split->node = NULL;
    char *start = text;
    char *end = text+strlen(text);
    printf("start = %s\n",start);
    while(start!=NULL){
        printf("333");
        char *temp = strstr(start,regex);
        if(temp!=NULL){
            TEXT_NODE *node = malloc(sizeof(TEXT_NODE));

            node->node = malloc(temp-start+1);
            node->next = NULL;
            memcpy(node->node,start,temp-start);
            node->node[temp-start] = 0;
printf("添加内容:%s\n",start);

            node->next = text_split->node;
            text_split->node = node;

        }
        else{

            TEXT_NODE *node = malloc(sizeof(TEXT_NODE));
            printf("长度:%d", end-start);
            printf("添加内容%s\n", start);
            node->node = malloc(end-start+1);
            node->next = NULL;
            memcpy(node->node,start,end-start);
            node->node[end-start] = 0;
            node->next = text_split->node;
            text_split->node = node;
            break;

        }
        start = temp+strlen(regex);
    }
    return text_split;
}

/*
 * 获取分割后字符串链表的长度
 */
int split_len(TEXT_SPLIT *text_split){
    int len = 0;
    TEXT_NODE *node = text_split->node;
    while(node){
        node = node->next;
        len++;
    };
    return len;
}

/*
 * 获取分割后字符串链表里第n个字符串
 */
char *split_get(TEXT_SPLIT *text_split, int n){
    int index = 0;
    TEXT_NODE *node = text_split->node;
    while(node){

        if(index==n){
            return node->node;
        }
        index++;
        node = node->next;
    }
    return NULL;
}

//输出分割后的字符列表
void split_print(TEXT_SPLIT *text_split){
    TEXT_NODE *node = text_split->node;
    while(node){
        printf("--> %s\n",node->node);
        node = node->next;
    }
}

//释放内存,你懂的
void split_free(TEXT_SPLIT *text_split){
    TEXT_NODE *node = text_split->node;
    while(node){
        free(node->node);
        TEXT_NODE *temp = node->next;
        free(node);
        node = temp;
    }
    free(text_split->text);
    free(text_split->regex);
    free(text_split);
}

int main(){
    printf("test");
    int i=0;
    //创建字符串分割结构 
    TEXT_SPLIT *text_split = create_split("风的影子|蟋蟀蝈蝈蛐蛐|火烧云|androis7|望尘11|清风沐竹|小钱","|");
    //循环获取字符串
    for( i=0;i<split_len(text_split);i++){
        printf("--> %s\n", split_get(text_split,i));
    }
    //释放内存 
    split_free(text_split);

return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注