CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  工具平台和程序库

字符串分解,内详

楼主linyd(走吧,看海去)2003-12-03 11:46:06 在 C/C++ / 工具平台和程序库 提问

char   X[]="a=AA,b=BB"  
   
  如何将AA从X中取出来? 问题点数:20、回复次数:14Top

1 楼daizh()回复于 2003-12-03 11:55:32 得分 0

strtoke()Top

2 楼byyyyy(苦行僧【苦】)回复于 2003-12-03 11:56:03 得分 0

char   xx[2];  
  xx[0]   =   X[2];  
  xx[1]   =   X[3];  
  这样可以嘛?Top

3 楼linyd(走吧,看海去)回复于 2003-12-03 11:57:22 得分 0

我要去等号于逗号之间的字符串Top

4 楼gdpglc(liancheng)回复于 2003-12-03 11:59:08 得分 0

char   X[]="a=AA,b=BB";  
  char   *str="AA";  
   
  for(int   i=0;i<sizeof(x);i++)  
  {  
      int   j=0,k=i;  
      for(;j<2;)  
      {  
          if(str[j]==X[k])        
              {  
                  j++;  
                  k++;  
              }else  
              break;    
      }  
    if(j==2)  
      {  
          找到;  
          break;    
      }        
  }Top

5 楼linyd(走吧,看海去)回复于 2003-12-03 12:28:26 得分 0

我要以等号和逗号为标志取之间的字符串Top

6 楼echolx(火凝风)回复于 2003-12-03 13:12:08 得分 5

char   X[]="a=AA,b=BB,";  
   
  char   *result;  
  for(int   i=0;i<sizeof(x);i++)  
  {  
      if(   x[i]==   '='   )  
      {  
                for(int   j=1;j+i<sizeof(x);j++)  
                {  
                  if(   x[i+j]==   ','   )  
                  {  
                    result=(char   *)malloc((j+1)*sizeof(char));  
                    strncpy(   result,x[i+1],   j-1   );  
                    printf(   "result=%s\n",   result   );  
                    }  
                    else  
                    {  
                              continue;  
                      }  
                  }  
      }  
      else  
      {  
                  contine;  
        }  
    }  
  Top

7 楼echolx(火凝风)回复于 2003-12-03 13:16:34 得分 5

刚才写的有些问题再做改进:  
  char   X[]="a=AA,b=BB,";  
   
  char   *result;  
  for(int   i=0;i<sizeof(x);i++)  
  {  
      if(   x[i]==   '='   )  
      {  
                for(int   j=1;j+i<sizeof(x);j++)  
                {  
                  if(   x[i+j]==   ','   )  
                  {  
                    result=(char   *)malloc((j+1)*sizeof(char));  
                    strncpy(   result,x[i+1],   j-1   );  
                    printf(   "result=%s\n",   result   );  
                    i=i+j;  
                    }  
                    else  
                    {  
                              continue;  
                      }  
                  }  
      }  
      else  
      {  
                  contine;  
        }  
    }  
   
  Top

8 楼Rossonero(奴家只求大官人快些)回复于 2003-12-03 13:19:58 得分 5

如果AA和BB是具体的变量,你可以考虑用sscanf  
  #include   <stdio.h>  
  int   main(void)  
  {  
  char   buf[]="a=34,b=78";  
  int   a,b;  
  sscanf(buf,"a=%d,b=%d",&a,&b);  
  printf("%d,%d",a,b);  
  return   0;  
  }Top

9 楼echolx(火凝风)回复于 2003-12-03 13:22:39 得分 0

还有点小问题,少写了个break,自己加上吧,不然会影响效率。Top

10 楼iicup(双杯献酒)回复于 2003-12-03 13:44:01 得分 0

同意  
  Rossonero(Maldini)Top

11 楼byyyyy(苦行僧【苦】)回复于 2003-12-03 17:09:39 得分 3

#include   <string.h>  
  #include   <stdio.h>  
   
  char   string[]   =   "a=AA,b=BB";  
  char   seps[]       =   "=,";  
  char   *token;  
   
  void   main(   void   )  
  {  
        printf(   "%s\n\nTokens:\n",   string   );  
   
        /*   Establish   string   and   get   the   first   token:   */  
        token   =   strtok(   string,   seps   );  
        while(   token   !=   NULL   )  
        {  
              /*   While   there   are   tokens   in   "string"   */      
              /*   Get   next   token:   */  
              if(strcmp(token,"AA")   ==   0)  
                    {  
                        printf(   "   %s\n",   token   );  
                        break;  
                    }    
              token   =   strtok(   NULL,   seps   );  
        }  
  }  
  Top

12 楼byyyyy(苦行僧【苦】)回复于 2003-12-03 17:15:56 得分 0

#include   <string.h>  
  #include   <stdio.h>  
   
  char   string[]   =   "a=AA,b=BB";  
  char   seps[]       =   "=,";  
  char   *token;  
   
  void   main(   void   )  
  {  
        printf(   "%s\n\nTokens:\n",   string   );  
   
        /*   Establish   string   and   get   the   first   token:   */  
        token   =   strtok(   string,   seps   );  
        while(   token   !=   NULL   )  
        {  
              /*   While   there   are   tokens   in   "string"   */      
              /*   Get   next   token:   */  
              if(strcmp(token,"AA")   ==   0)  
                    {  
                        printf(   "   %s\n",   token   );  
                        break;  
                    }    
              token   =   strtok(   NULL,   seps   );  
        }  
  }  
  Top

13 楼muymuy(muy)回复于 2003-12-03 18:30:02 得分 2

 
  注意:  
  strtok()函数会使用一个静态变量,因此,不可用于多线程程序中。  
   
  Warning:   Each   of   these   functions   uses   a   static   variable   for   parsing   the   string   into   tokens.   If   multiple   or   simultaneous   calls   are   made   to   the   same   function,   a   high   potential   for   data   corruption   and   inaccurate   results   exists.   Therefore,   do   not   attempt   to   call   the   same   function   simultaneously   for   different   strings   and   be   aware   of   calling   one   of   these   functions   from   within   a   loop   where   another   routine   may   be   called   that   uses   the   same   function.     However,   calling   this   function   simultaneously   from   multiple   threads   does   not   have   undesirable   effects.  
   
   
  Top

14 楼aiqin(来无影)回复于 2003-12-12 13:00:33 得分 0

#include   <iostream.h>  
  #include   <stirng.h>  
   
  void   test(char*   str)  
  {  
  int   a[100];  
  int   digit=0;  
  char   *p   =   str;  
  int   k=1;  
  int   j   =   0;  
  bool   begin   =   FALSE;  
   
  while(*p   !=   '\0')  
  {  
  if(*p   ==   '=')  
  {  
  begin   =   TRUE;  
  p++;  
  continue;  
  }  
  if(begin)  
  {  
  if(   *p   <=   '9'   &&   *p   >=   '0'   )  
  {  
   
  digit   =   digit*k   +   (*p   -   48);  
  k   *=   10;  
  }  
  else  
  {  
  k   =   1;  
  a[j++]   =   digit;  
  cout   <<   digit   <<   endl;  
  digit   =   0;  
  begin   =   FALSE;  
  }  
  }  
   
  p++;  
  }  
  if(begin)  
  cout   <<   digit   <<   endl;  
  }  
   
  int   main(int   argc,   char*   argv[])  
  {  
  char   str[]   =   "a=34,b=21,b=";  
  test(str);  
  return   0;  
  }Top

相关问题

  • 分解字符串
  • 分解字符串
  • 字符串分解
  • 怎么替换字符串阿?(内详)
  • 如何分解字符串?
  • 字符串分解问题???
  • 如何分解字符串?
  • oracle中分解字符串
  • 字符串分解问题
  • 如何分解字符串?

关键词

  • null
  • tokens
  • strtok
  • aa
  • simultaneously
  • seps
  • digit
  • bb
  • xx
  • same

得分解答快速导航

  • 帖主:linyd
  • echolx
  • echolx
  • Rossonero
  • byyyyy
  • muymuy

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo