免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: abcbuzhiming

[函数] 我想知道各位高手你们是用什么办法完成分割和替换字符串操作的? [复制链接]

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
发表于 2011-03-02 17:14 |显示全部楼层
性能?
yulihua49 发表于 2011-03-02 14:33



我也贡献一个字符串替换,大家用吧。

  1. /************************************************************************
  2. *function:strsubst 替换字符串函数              
  3. *description:用'str'替换'from'的前cnt个字符   
  4. * 返回指向替换后边的字节。
  5. ************************************************************************/
  6. char *strsubst(char *from,int cnt,char *str)
  7. {
  8.         int i;
  9.         register char *cp, *cp1, *cp2;

  10.         if(!from)
  11.                 return 0;
  12.         i = strlen(from);
  13.         if(cnt < 0)
  14.                 cnt = 0;
  15.         else if(cnt > i)
  16.                 cnt = i;
  17.         else ;
  18.         i = str ? strlen(str) : 0;
  19.         if(i < cnt)                             /* delete some char*/
  20.         {
  21.                 cp1 = from + i;
  22.                 cp = from + cnt;
  23.                 while(*cp)
  24.                         *cp1++ = *cp++;
  25.                 *cp1 = 0;
  26.         }
  27.         else if (i > cnt)                       /* extend some*/
  28.         {
  29.                 cp2 = from + cnt;
  30.                 cp = from + strlen(from);
  31.                 cp1 = cp + i - cnt;
  32.                 while(cp >= cp2)
  33.                         *cp1-- = *cp--;
  34.         }
  35.         else ;
  36.         if(str)
  37.                 strncpy(from, str, i);

  38.         return (from + i);
  39. }
复制代码

论坛徽章:
0
发表于 2011-03-02 18:24 |显示全部楼层
本帖最后由 bittertea 于 2011-03-02 18:31 编辑

字符串分割的, 来自google code search, 后来根据这个改了一版带split后执行回调的给自己用:
  1. #include <stdio.h>
  2. #include <string.h>

  3. /*
  4. - split - divide a string into fields, like awk split()
  5. = int split(char *string, char *fields[], int nfields, char *sep);
  6. */
  7. int                             /* number of fields, including overflow */
  8. split(string, fields, nfields, sep)
  9. char *string;
  10. char *fields[];                 /* list is not NULL-terminated */
  11. int nfields;                    /* number of entries available in fields[] */
  12. char *sep;                      /* "" white, "c" single char, "ab" [ab]+ */
  13. {
  14.         register char *p = string;
  15.         register char c;                        /* latest character */
  16.         register char sepc = sep[0];
  17.         register char sepc2;
  18.         register int fn;
  19.         register char **fp = fields;
  20.         register char *sepp;
  21.         register int trimtrail;

  22.         /* white space */
  23.         if (sepc == '\0') {
  24.                 while ((c = *p++) == ' ' || c == '\t')
  25.                         continue;
  26.                 p--;
  27.                 trimtrail = 1;
  28.                 sep = " \t";    /* note, code below knows this is 2 long */
  29.                 sepc = ' ';
  30.         } else
  31.                 trimtrail = 0;
  32.         sepc2 = sep[1];         /* now we can safely pick this up */

  33.         /* catch empties */
  34.         if (*p == '\0')
  35.                 return(0);

  36.         /* single separator */
  37.         if (sepc2 == '\0') {
  38.                 fn = nfields;
  39.                 for (;;) {
  40.                         *fp++ = p;
  41.                         fn--;
  42.                         if (fn == 0)
  43.                                 break;
  44.                         while ((c = *p++) != sepc)
  45.                                 if (c == '\0')
  46.                                         return(nfields - fn);
  47.                         *(p-1) = '\0';
  48.                 }
  49.                 /* we have overflowed the fields vector -- just count them */
  50.                 fn = nfields;
  51.                 for (;;) {
  52.                         while ((c = *p++) != sepc)
  53.                                 if (c == '\0')
  54.                                         return(fn);
  55.                         fn++;
  56.                 }
  57.                 /* not reached */
  58.         }

  59.         /* two separators */
  60.         if (sep[2] == '\0') {
  61.                 fn = nfields;
  62.                 for (;;) {
  63.                         *fp++ = p;
  64.                         fn--;
  65.                         while ((c = *p++) != sepc && c != sepc2)
  66.                                 if (c == '\0') {
  67.                                         if (trimtrail && **(fp-1) == '\0')
  68.                                                 fn++;
  69.                                         return(nfields - fn);
  70.                                 }
  71.                         if (fn == 0)
  72.                                 break;
  73.                         *(p-1) = '\0';
  74.                         while ((c = *p++) == sepc || c == sepc2)
  75.                                 continue;
  76.                         p--;
  77.                 }
  78.                 /* we have overflowed the fields vector -- just count them */
  79.                 fn = nfields;
  80.                 while (c != '\0') {
  81.                         while ((c = *p++) == sepc || c == sepc2)
  82.                                 continue;
  83.                         p--;
  84.                         fn++;
  85.                         while ((c = *p++) != '\0' && c != sepc && c != sepc2)
  86.                                 continue;
  87.                 }
  88.                 /* might have to trim trailing white space */
  89.                 if (trimtrail) {
  90.                         p--;
  91.                         while ((c = *--p) == sepc || c == sepc2)
  92.                                 continue;
  93.                         p++;
  94.                         if (*p != '\0') {
  95.                                 if (fn == nfields+1)
  96.                                         *p = '\0';
  97.                                 fn--;
  98.                         }
  99.                 }
  100.                 return(fn);
  101.         }

  102.         /* n separators */
  103.         fn = 0;
  104.         for (;;) {
  105.                 if (fn < nfields)
  106.                         *fp++ = p;
  107.                 fn++;
  108.                 for (;;) {
  109.                         c = *p++;
  110.                         if (c == '\0')
  111.                                 return(fn);
  112.                         sepp = sep;
  113.                         while ((sepc = *sepp++) != '\0' && sepc != c)
  114.                                 continue;
  115.                         if (sepc != '\0')       /* it was a separator */
  116.                                 break;
  117.                 }
  118.                 if (fn < nfields)
  119.                         *(p-1) = '\0';
  120.                 for (;;) {
  121.                         c = *p++;
  122.                         sepp = sep;
  123.                         while ((sepc = *sepp++) != '\0' && sepc != c)
  124.                                 continue;
  125.                         if (sepc == '\0')       /* it wasn't a separator */
  126.                                 break;
  127.                 }
  128.                 p--;
  129.         }

  130.         /* not reached */
  131. }

  132. #ifdef TEST_SPLIT


  133. /*
  134. * test program
  135. * pgm          runs regression
  136. * pgm sep      splits stdin lines by sep
  137. * pgm str sep  splits str by sep
  138. * pgm str sep n        splits str by sep n times
  139. */
  140. int
  141. main(argc, argv)
  142. int argc;
  143. char *argv[];
  144. {
  145.         char buf[512];
  146.         register int n;
  147. #       define  MNF     10
  148.         char *fields[MNF];

  149.         if (argc > 4)
  150.                 for (n = atoi(argv[3]); n > 0; n--) {
  151.                         (void) strcpy(buf, argv[1]);
  152.                 }
  153.         else if (argc > 3)
  154.                 for (n = atoi(argv[3]); n > 0; n--) {
  155.                         (void) strcpy(buf, argv[1]);
  156.                         (void) split(buf, fields, MNF, argv[2]);
  157.                 }
  158.         else if (argc > 2)
  159.                 dosplit(argv[1], argv[2]);
  160.         else if (argc > 1)
  161.                 while (fgets(buf, sizeof(buf), stdin) != NULL) {
  162.                         buf[strlen(buf)-1] = '\0';      /* stomp newline */
  163.                         dosplit(buf, argv[1]);
  164.                 }
  165.         else
  166.                 regress();

  167.         exit(0);
  168. }

  169. dosplit(string, seps)
  170. char *string;
  171. char *seps;
  172. {
  173. #       define  NF      5
  174.         char *fields[NF];
  175.         register int nf;

  176.         nf = split(string, fields, NF, seps);
  177.         print(nf, NF, fields);
  178. }

  179. print(nf, nfp, fields)
  180. int nf;
  181. int nfp;
  182. char *fields[];
  183. {
  184.         register int fn;
  185.         register int bound;

  186.         bound = (nf > nfp) ? nfp : nf;
  187.         printf("%d:\t", nf);
  188.         for (fn = 0; fn < bound; fn++)
  189.                 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
  190. }

  191. #define RNF     5               /* some table entries know this */
  192. struct {
  193.         char *str;
  194.         char *seps;
  195.         int nf;
  196.         char *fi[RNF];
  197. } tests[] = {
  198.         "",             " ",    0,      { "" },
  199.         " ",            " ",    2,      { "", "" },
  200.         "x",            " ",    1,      { "x" },
  201.         "xy",           " ",    1,      { "xy" },
  202.         "x y",          " ",    2,      { "x", "y" },
  203.         "abc def  g ",  " ",    5,      { "abc", "def", "", "g", "" },
  204.         "  a bcd",      " ",    4,      { "", "", "a", "bcd" },
  205.         "a b c d e f",  " ",    6,      { "a", "b", "c", "d", "e f" },
  206.         " a b c d ",    " ",    6,      { "", "a", "b", "c", "d " },

  207.         "",             " _",   0,      { "" },
  208.         " ",            " _",   2,      { "", "" },
  209.         "x",            " _",   1,      { "x" },
  210.         "x y",          " _",   2,      { "x", "y" },
  211.         "ab _ cd",      " _",   2,      { "ab", "cd" },
  212.         " a_b  c ",     " _",   5,      { "", "a", "b", "c", "" },
  213.         "a b c_d e f",  " _",   6,      { "a", "b", "c", "d", "e f" },
  214.         " a b c d ",    " _",   6,      { "", "a", "b", "c", "d " },

  215.         "",             " _~",  0,      { "" },
  216.         " ",            " _~",  2,      { "", "" },
  217.         "x",            " _~",  1,      { "x" },
  218.         "x y",          " _~",  2,      { "x", "y" },
  219.         "ab _~ cd",     " _~",  2,      { "ab", "cd" },
  220.         " a_b  c~",     " _~",  5,      { "", "a", "b", "c", "" },
  221.         "a b_c d~e f",  " _~",  6,      { "a", "b", "c", "d", "e f" },
  222.         "~a b c d ",    " _~",  6,      { "", "a", "b", "c", "d " },

  223.         "",             " _~-", 0,      { "" },
  224.         " ",            " _~-", 2,      { "", "" },
  225.         "x",            " _~-", 1,      { "x" },
  226.         "x y",          " _~-", 2,      { "x", "y" },
  227.         "ab _~- cd",    " _~-", 2,      { "ab", "cd" },
  228.         " a_b  c~",     " _~-", 5,      { "", "a", "b", "c", "" },
  229.         "a b_c-d~e f",  " _~-", 6,      { "a", "b", "c", "d", "e f" },
  230.         "~a-b c d ",    " _~-", 6,      { "", "a", "b", "c", "d " },

  231.         "",             "  ",   0,      { "" },
  232.         " ",            "  ",   2,      { "", "" },
  233.         "x",            "  ",   1,      { "x" },
  234.         "xy",           "  ",   1,      { "xy" },
  235.         "x y",          "  ",   2,      { "x", "y" },
  236.         "abc def  g ",  "  ",   4,      { "abc", "def", "g", "" },
  237.         "  a bcd",      "  ",   3,      { "", "a", "bcd" },
  238.         "a b c d e f",  "  ",   6,      { "a", "b", "c", "d", "e f" },
  239.         " a b c d ",    "  ",   6,      { "", "a", "b", "c", "d " },

  240.         "",             "",     0,      { "" },
  241.         " ",            "",     0,      { "" },
  242.         "x",            "",     1,      { "x" },
  243.         "xy",           "",     1,      { "xy" },
  244.         "x y",          "",     2,      { "x", "y" },
  245.         "abc def  g ",  "",     3,      { "abc", "def", "g" },
  246.         "\t a bcd",     "",     2,      { "a", "bcd" },
  247.         "  a \tb\t c ", "",     3,      { "a", "b", "c" },
  248.         "a b c d e ",   "",     5,      { "a", "b", "c", "d", "e" },
  249.         "a b\tc d e f", "",     6,      { "a", "b", "c", "d", "e f" },
  250.         " a b c d e f ",        "",     6,      { "a", "b", "c", "d", "e f " },

  251.         NULL,           NULL,   0,      { NULL },
  252. };

  253. regress()
  254. {
  255.         char buf[512];
  256.         register int n;
  257.         char *fields[RNF+1];
  258.         register int nf;
  259.         register int i;
  260.         register int printit;
  261.         register char *f;

  262.         for (n = 0; tests[n].str != NULL; n++) {
  263.                 (void) strcpy(buf, tests[n].str);
  264.                 fields[RNF] = NULL;
  265.                 nf = split(buf, fields, RNF, tests[n].seps);
  266.                 printit = 0;
  267.                 if (nf != tests[n].nf) {
  268.                         printf("split `%s' by `%s' gave %d fields, not %d\n",
  269.                                 tests[n].str, tests[n].seps, nf, tests[n].nf);
  270.                         printit = 1;
  271.                 } else if (fields[RNF] != NULL) {
  272.                         printf("split() went beyond array end\n");
  273.                         printit = 1;
  274.                 } else {
  275.                         for (i = 0; i < nf && i < RNF; i++) {
  276.                                 f = fields[i];
  277.                                 if (f == NULL)
  278.                                         f = "(NULL)";
  279.                                 if (strcmp(f, tests[n].fi[i]) != 0) {
  280.                                         printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
  281.                                                 tests[n].str, tests[n].seps,
  282.                                                 i, fields[i], tests[n].fi[i]);
  283.                                         printit = 1;
  284.                                 }
  285.                         }
  286.                 }
  287.                 if (printit)
  288.                         print(nf, RNF, fields);
  289.         }
  290. }
  291. #endif
复制代码
根据那个改的带split执行回调的,:
  1. //定义拆分后单元结构
  2. typedef struct field
  3. {
  4.     char *value;
  5.     unsigned int len;
  6. } field_t;

  7. #define SPLIT_CB_OK (0)
  8. #define SPLIT_CB_ERR (-1)

  9. //拆分成功后回调
  10. typedef int (*split_cb_t) (field_t *fields, unsigned int nfields, void *arg);

  11. #define my_split(string, sep, fields, nfields) \
  12.     my_split2(string, sep, fields, nfields, NULL, NULL)

  13. /*函数名称: my_split2
  14. *说    明: 拆分字符串
  15. *参    数: string  母串
  16. *          sep     子串
  17. *          fields  保存拆分后的单元结构数组
  18. *          nfields 该数组个数
  19. *          split_cb拆分成功回调
  20. *          arg     回调函数用户数据
  21. *返    回: 拆分后的单元个数
  22. */
  23. unsigned int my_split2(char *string, char *sep, field_t fields[],
  24.     unsigned int nfields, split_cb_t split_cb, void *arg)
  25. {
  26.     char *p = string;
  27.     char c;
  28.     char sepc;
  29.     char sepc2;
  30.     unsigned int fn;
  31.     field_t *fp = fields;
  32.     char *sepp;
  33.     int trimtrail;

  34.     if (!p || *p == '\0')
  35.         return 0;

  36.     if (!sep || sep[0] == '\0')
  37.     {
  38.         //未指定分隔字符, 则去掉开始的white space
  39.         while ((c = *p) == ' ' || c == '\t')
  40.         {
  41.             p++;
  42.             continue;
  43.         }
  44.         trimtrail = 1;
  45.         sep = " \t";    //分隔字符为" \t"
  46.     }
  47.     else
  48.         trimtrail = 0;

  49.     sepc = sep[0];
  50.     sepc2 = sep[1];

  51.     if (sepc2 == '\0')
  52.     {
  53.         //分隔字符为单字符
  54.         fn = nfields;
  55.         for (;;)
  56.         {
  57.             fp -> value = p;
  58.             fn--;
  59.             while ((c = *p) != sepc)
  60.             {
  61.                 if (c == '\0')
  62.                 {
  63.                     fp -> len = p - fp -> value;
  64.                     do_split_cb(split_cb, fp, nfields - fn, arg);
  65.                     return (nfields - fn);
  66.                 }
  67.                 p++;
  68.             }

  69.             fp -> len = p - fp -> value; //可能为0
  70.             do_split_cb(split_cb, fp, nfields - fn, arg);
  71.             p++;

  72.             if (fn == 0)
  73.                 break;

  74.             fp++;
  75.         }

  76.         //若仍有未处理的字符串, 则返回总token个数
  77.         fn = nfields;
  78.         for (;;)
  79.         {
  80.             while ((c = *p) != sepc)
  81.             {
  82.                 if (c == '\0') return fn;
  83.                 p++;
  84.             }
  85.             fn++;
  86.         }
  87.     }

  88.     if (sep[2] == '\0')
  89.     {
  90.         //分隔字符串含有两个字符
  91.         fn = nfields;
  92.         for (; ; )
  93.         {
  94.             fp -> value = p;
  95.             fn--;
  96.             while ((c = *p) != sepc && c != sepc2)
  97.             {
  98.                 if (c == '\0')
  99.                 {
  100.                     if (trimtrail && (fp -> value) == '\0')
  101.                     {
  102.                         fn++;
  103.                     }
  104.                     else
  105.                     {
  106.                         fp -> len = p - fp -> value;
  107.                         do_split_cb(split_cb, fp, nfields - fn, arg);
  108.                     }
  109.                     return (nfields - fn);
  110.                 }
  111.                 p++;
  112.             }

  113.             fp -> len = p - fp -> value;
  114.             do_split_cb(split_cb, fp, nfields - fn, arg);
  115.             p++;

  116.             if (fn == 0)
  117.                 break;

  118.             fp++;

  119.             while ((c = *p) == sepc || c == sepc2)
  120.             {
  121.                 p++;
  122.                 continue;
  123.             }
  124.         }

  125.         //若仍有未处理的字符串, 则返回总token个数
  126.         fn = nfields;
  127.         while (c != '\0')
  128.         {
  129.             while ((c = *p) == sepc || c == sepc2)
  130.             {
  131.                 p++;
  132.                 continue;
  133.             }

  134.             fn++;
  135.             while ((c = *p) != '\0' && c != sepc && c != sepc2)
  136.             {
  137.                 p++;
  138.                 continue;
  139.             }
  140.             p++;
  141.         }

  142.         p--; //p指向'\0'
  143.         if (trimtrail)
  144.         {
  145.             p--; //p指向'\0'前一个字节
  146.             while ((c = *p) == sepc || c == sepc2)
  147.             {
  148.                 //去除尾部white space
  149.                 p--;
  150.                 continue;
  151.             }

  152.             p++;
  153.             if (*p != '\0')
  154.             {
  155.                 //尾部有white space
  156.                 fn--;
  157.             }
  158.         }

  159.         return fn;
  160.     }

  161.     //分隔字符串含有多个字符
  162.     fn = 0;
  163.     for (; ; )
  164.     {
  165.         if (fn < nfields)
  166.         {
  167.             fp -> value = p;
  168.             //*fp++ = p;
  169.         }

  170.         fn++;
  171.         for (; ; )
  172.         {
  173.             c = *p; //获取母串当前字符
  174.             if (c == '\0')
  175.             {
  176.                 if (fn <= nfields)
  177.                 {
  178.                     fp -> len = p - fp -> value;
  179.                     do_split_cb(split_cb, fp, fn, arg);
  180.                 }
  181.                 return fn;
  182.             }

  183.             //匹配当前字符是否在分隔符中
  184.             sepp = sep;
  185.             while ((sepc = *sepp) != '\0' && sepc != c)
  186.             {
  187.                 sepp++;
  188.                 continue;
  189.             }

  190.             //母串中遇到分隔符则跳出循环, 否则母串字符指针递增
  191.             if (sepc != '\0')
  192.                 break;
  193.             p++;
  194.         }

  195.         if (fn <= nfields)
  196.         {
  197.             fp -> len = p - fp -> value;
  198.             do_split_cb(split_cb, fp, nfields - fn, arg);
  199.             fp++;
  200.         }

  201.         p++;
  202.         for (; ; )
  203.         {
  204.             //略去母串中连续得分隔符
  205.             c = *p;
  206.             sepp = sep;
  207.             while ((sepc = *sepp) != '\0' && sepc != c)
  208.             {
  209.                 sepp++;
  210.                 continue;
  211.             }

  212.             if (sepc == '\0')
  213.                 break;
  214.             p++;
  215.         }
  216.     }

  217.     /* not reached */
  218. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-03-02 19:35 |显示全部楼层
我的工作也经常需要处理字符串,所以我早改 Perl 了。

论坛徽章:
0
发表于 2011-03-02 21:25 |显示全部楼层
我的工作也经常需要处理字符串,所以我早改 Perl 了。
flw 发表于 2011-03-02 19:35


有没有用某种处理字符串很强的语言,比如你说的这种,嵌入到C/C++程序里的办法,或者说,用C/C++程序去调用这种语言的功能,但是最终程序仍然是一个程序不需要外带其它东西等等

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-03-02 21:27 |显示全部楼层
有没有用某种处理字符串很强的语言,比如你说的这种,嵌入到C/C++程序里的办法,或者说,用C/C++程序去 ...
abcbuzhiming 发表于 2011-03-02 21:25

就是 Perl 啊。
libperl.a

gcc -lperl

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
发表于 2011-03-03 12:34 |显示全部楼层
本帖最后由 cobras 于 2011-03-03 13:19 编辑

贡献一个字串替换函数。采用双缓冲。
  1. #include <string.h>

  2. /*
  3. DESCRIPTION

  4.     replace the specified string of one text to be another string

  5. PARAMETERS

  6.     text      [in, not null]
  7.               source text
  8.     tar       [in, not null]
  9.               which string to search
  10.     rep       [in, not null]
  11.               replace to which string
  12.     buf       [out, can null]
  13.               buffer to store the result
  14.               can't be text itself, except that
  15.               tar length is not less than rep length
  16.     buf_len   [in, positive]
  17.               buffer storage max length, include terminator '\0'
  18.     o_len     [out, can null]
  19.               result text length

  20. RETURNS

  21.     NULL      ERROR: buffer can't enough to store the result
  22.               or buf parameter is nul
  23.     buf       to support link expression
  24. */
  25. char *str_replace(const char *text, const char *tar, const char *rep, char *buf, int buf_len, int *o_len)
  26. {
  27.         int tar_len, rep_len, len;
  28.         int i;
  29.         char *str;

  30.         tar_len = strlen(tar);
  31.         rep_len = strlen(rep);
  32.         if (rep_len <= tar_len || text != buf) {
  33.                 i = 0;
  34.                 while (*text != '\0') {
  35.                         str = strstr(text, tar);
  36.                         len = str == NULL ? strlen(text) : str - text;
  37.                         if (len > 0) {
  38.                                 if (buf != NULL && i + len < buf_len) {
  39.                                         strncpy(buf + i, text, len);
  40.                                 }
  41.                                 i += len;
  42.                                 text += len;
  43.                         }
  44.                         if (str != NULL) {
  45.                                 if (buf != NULL && i + rep_len < buf_len) {
  46.                                         strncpy(buf + i, rep, rep_len);
  47.                                 }
  48.                                 i += rep_len;
  49.                                 text += tar_len;
  50.                         }
  51.                 }
  52.                 if (o_len != NULL) {
  53.                         *o_len = i;
  54.                 }
  55.                 if (i < buf_len) {
  56.                         buf[i] = '\0';
  57.                         return buf;
  58.                 }
  59.         }
  60.         return NULL;
  61. }

  62. #include <stdio.h>

  63. int main(void)
  64. {
  65.         char buf[256];
  66.         /* TEST1: test use double buffer */
  67.         if (str_replace("i love you, you love me?", "you", "my darling", buf, sizeof(buf), NULL) != NULL) {
  68.                 printf("TEST1: %s\n", buf);
  69.         }
  70.         strcpy(buf, "abcdefghijklmnopqrstuvwxyz");
  71.         /* TEST2: test use same buffer */
  72.         if (str_replace(buf, "xyz", "__", buf, strlen(buf) + 1, NULL) != NULL) {
  73.                 printf("TEST2: %s\n", buf);
  74.         }
  75.         return 0;
  76. }
复制代码

论坛徽章:
0
发表于 2011-03-03 18:46 |显示全部楼层
c 处理的确不好 建议用 shell 中处理字符串的函数或者 awk sed 都可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP