字符串分解,内详
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




