#include <stdio.h> #include <string.h> #include <stdlib.h> /*删除在src中的str串*/ char *strdel(char *src, char *str) { if(NULL == src || NULL == str) { return NULL; } char *pd = strstr(src, str); if(NULL == pd) { return NULL; } int offset = pd - src; strcpy(&src[offset], &src[offset + strlen(str)]); return src; } /*在指定位置插入串*/ char *strins(char *src, int pos, char *ins) { if(NULL == src || NULL == ins) { return NULL; } int src_len = strlen(src); if(pos > src_len) { return NULL; } int ins_len = strlen(ins); char * st = (char *)malloc(sizeof(char)*(src_len - ins_len)); strcpy(st, &src[pos]); strcpy(&src[pos + ins_len], st); free(st); st = NULL; strnset(&src[pos], ' ', ins_len); memcpy(&src[pos], ins, ins_len); return src; } /*替换,将src中的dest串替换成rep*/ char *strrep(char *src, char *dest, char *rep) { if(NULL == src || NULL == dest || NULL == rep) { return NULL; } char *pd = strstr(src, dest); if(NULL == pd) { return NULL; } int offset = pd - src; if(NULL == strdel(src, dest)) { return NULL; } if(NULL == strins(src, offset, rep)) { return NULL; } return src; } /*替换所有src中出现的dest为rep*/ char *strallrep(char *src, char *dest, char *rep) { if(NULL == src || NULL == dest || NULL == rep) return NULL; while(strrep(src, dest, rep) != NULL) { } return src; } /*去除字符串前后空格*/ char *strtrim(char *str) { if(NULL == str) return NULL; int i = 0; while(str[i] == ' ') { i++; } strcpy(&str[0], &str[i]); i = strlen(str) - 1; while(str[i] == ' ') { //printf("%c ", str[i]); i--; } str[i+1] = '\0'; return str; } /*取得字符串哈希值*/ unsigned int strhash(char *str) { unsigned int seed = 131; unsigned int hashval = 0; char *tempstr = str; while(*tempstr){ hashval = hashval * seed + (*tempstr++); } return (hashval & 0x7FFFFFFF); } /*获取src串中的label值*/ char *strhtmllabel(char *src, char *label) { if(NULL == src || NULL == label) return NULL; char *tmp = (char *)malloc(sizeof(char) * strlen(label)+10); strcpy(tmp, label); strins(tmp, 0, "<"); strins(tmp, strlen(tmp) , ">"); //printf("tmp = %s\n", tmp); char *beg_p = strstr(src, tmp); if(NULL == beg_p) { free(tmp); tmp = NULL; return NULL; } strins(tmp, 1, "/"); char *end_p = strstr(src, tmp); if(NULL == end_p) { free(tmp); tmp = NULL; return NULL; } beg_p = &beg_p[strlen(tmp)-1]; *end_p = '\0'; free(tmp); tmp = NULL; return beg_p; } /*直接将文件读入*/ char *strgetfiletext(const char* path) { char *filename = path; FILE *f = fopen(filename, "rb"); fseek(f, 0, SEEK_END); long len = ftell(f); fseek(f, 0, SEEK_SET); char *data = (char *) malloc(len + 1); fread(data, 1, len, f); fclose(f); f = NULL; return data; } int main() { char *str1 = "hello world!"; char str2[300] = {0}; //strcpy 拷贝 strcpy(str2, str1);//strncpy(str2, str1, strlen(str1)); printf("%s\n", str2); //strrev 转置 strrev(str2); printf("%s\n", str2); strrev(str2); //strstr 取得指定字串的指针 char *p = strstr(str2, "world"); printf("%s\n",p); //sscanf char* 转 int等一切 char *str_num = "123"; int num = 0; sscanf(str_num, "%d", &num); printf("num = %d\n", num); //sprintf int等一切 转 char* int val = 456; char str_val[30] = {30}; sprintf(str_val, "%d", val); printf("str_val = %s\n", str_val); //strtok 查找以X串分割开的串 注意:strtok会修改str2 printf("strtok = %s\n", strtok(str2, " ")); char *cp = NULL; while((cp = strtok(NULL, " ")) != NULL) printf("strtok = %s\n", cp); strset(str2, 0x00); strcpy(str2, str1); //strupr 转大写字母 strupr(str2); printf("strupr : %s\n", str2); //stricmp 忽略大小写比较 printf("stricmp = %d\n", stricmp(str1, str2)); //strlwr 转小写字母 strlwr(str2); printf("strlwr : %s\n", str2); //strset/strnset 将串整体赋值 strset(str2, 0x00);//strnset(str2, 0x00, 300); printf("%s\n", str2); //替换 strcpy(str2, str1); strallrep(str2, "l", " "); printf("%s\n", str2); //去除前后空格 strcpy(str2, " hello! "); strtrim(str2); printf("strtrim:%s,%d\n", str2, strlen(str2)); //字符串哈希 strcpy(str2, "hello!"); printf("hashval = %d\n", strhash(str2)); strcpy(str2, "hello world!"); printf("hashval = %d\n", strhash(str2)); strcpy(str2, "hello!"); printf("hashval = %d\n", strhash(str2)); //取html标签 strcpy(str2, "<html><div>hello!</div></html>"); printf("strhtmllabel:%s\n",strhtmllabel(str2, "div")); return 0; }