user-0 提问: 字符串截取

回复:
user: user-1
content: 引用 18 楼 Intel0011 的回复:void GetField(char *czData, char *outData) {    sscanf(czData, "%*[^;];NB=%[^;]", outData); }  解释一下 %[^;]表示匹配到非;符号为止 *号表示不存储所匹配的值 合起来就是 %*[^;]不存储匹配到非;符号为止的值 ;NB=表示匹配;NB=符号 %[^;]表示匹配到非;符号为止的值，并存储到outData中

user: user-1
content: void GetField(char *czData, char *outData) {    sscanf(czData, "%*[^;];NB=%[^;]", outData); }

user: user-2
content: 引用 8 楼 xuzhouweihao 的回复:, sscanf(czData,"NB=%[^;];",outdata);[/quote]  骚年，你测试了没？

user: user-3
content: 需要那么麻烦吗？  用 strstr  就好了呗

user: user-4
content: 上帖回错了。

user: user-4
content: #include <stdio.h> #include <stdlib.h> #include <malloc.h> int **p; int **newarr2d(int rows,int cols) {     int **p,i;      p=(int **)malloc(rows*sizeof(int *));     if (NULL==p) exit(1);     for (i=0;i<rows;i++) {         p[i]=(int *)malloc(cols*sizeof(int));         if (NULL==p[i]) exit(1);     }     return p; } void deletearr2d(int **p,int rows) {     int i;      for (i=0;i<rows;i++) {         free(p[i]);     }     free(p); } int main() {     int **arr2d,i,j,r,c;      r=4;     c=5;     //在堆中开辟一个4×5的二维int数组     arr2d=newarr2d(r,c);     for (i=0;i<r;i++) {         for (j=0;j<c;j++) {             arr2d[i][j]=i*c+j;         }     }     for (i=0;i<r;i++) {         for (j=0;j<c;j++) {             printf(" %2d",arr2d[i][j]);         }         printf("\n");     }     deletearr2d(arr2d,r);      r=6;     c=3;     //在堆中开辟一个6×3的二维int数组     arr2d=newarr2d(r,c);     for (i=0;i<r;i++) {         for (j=0;j<c;j++) {             arr2d[i][j]=i*c+j;         }     }     for (i=0;i<r;i++) {         for (j=0;j<c;j++) {             printf(" %2d",arr2d[i][j]);         }         printf("\n");     }     deletearr2d(arr2d,r);      return 0; } //  0  1  2  3  4 //  5  6  7  8  9 // 10 11 12 13 14 // 15 16 17 18 19 //  0  1  2 //  3  4  5 //  6  7  8 //  9 10 11 // 12 13 14 // 15 16 17 //

user: user-5
content: void main() { 	char* czData = "SN=12346;NB=abc;TB=688;"; 	char outData[255] = {0}; 	GetField(czData, "NB=", outData); 	printf("%s", outData); }  void GetField(char* czData,char* key,char* outData) { 	char *keyPos = strstr(czData, key);//寻找源串中Key首次出现的位置	  	//未找到 	if(keyPos == NULL)			 		return;	  	//找到 	char *semPos = strstr(keyPos, ";");//寻找key后首次出现;的位置 	char *equPos = strstr(keyPos, "=");//寻找key后首次出现=的位置 	char *outPos = equPos+1;//=后面就是我们要的内容			 	strncpy(outData, outPos, semPos-outPos);//复制这部分内容 }

user: user-6
content: 引用 9 楼 zhao4zhong1 的回复:[quote=引用 7 楼 xuzhouweihao 的回复:] [quote=引用 6 楼 xuzhouweihao 的回复:] 使用 sscanf(czData,"NB=%[^;],outdata);  这样就可以了吧  少了个" sscanf(czData,"NB=%[^;];“,outdata);[/quote]   char outdata[40]; char *p; p=strstr(czData,";NB="); if  (p) {  if (1==sscanf(p,";NB=%39[^;];“,outdata)) {   printf("NB=%s\n",outdata);  } } [/quote] 是的，最好加一个截取字符串的长度。

user: user-4
content: 引用 7 楼 xuzhouweihao 的回复:[quote=引用 6 楼 xuzhouweihao 的回复:] 使用 sscanf(czData,"NB=%[^;],outdata);  这样就可以了吧  少了个" sscanf(czData,"NB=%[^;];“,outdata);[/quote]   char outdata[40]; char *p; p=strstr(czData,";NB="); if  (p) {  if (1==sscanf(p,";NB=%39[^;];“,outdata)) {   printf("NB=%s\n",outdata);  } }

user: user-6
content: , sscanf(czData,"NB=%[^;];",outdata);[/quote]

user: user-6
content: 引用 6 楼 xuzhouweihao 的回复:使用 sscanf(czData,"NB=%[^;],outdata);  这样就可以了吧  少了个" sscanf(czData,"NB=%[^;];“,outdata);

user: user-6
content: 使用 sscanf(czData,"NB=%[^;],outdata);  这样就可以了吧

user: user-7
content: 引用 2 楼 brookmill 的回复:int GetField(char *czData, char *strSearch, char *outData) {          char *p;         if (czData == NULL || strSearch == NULL || outData == NULL) {                 return -1;         }         p = strstr(czData, strSearch);         if (p == NULL) {                 return -1;         }         while (*p != '=' && *p != '\0') {                 p++;         }         if (*p == '\0') {                 return -1;         }         p++;         while (*p != ';' && *p != '\0') {                 *outData++ = *p++;         }         *outData = '\0';         return 0; }

user: user-8
content: 引用 2 楼 brookmill 的回复:int GetField(char *czData, char *strSearch, char *outData) {          char *p;         if (czData == NULL || strSearch == NULL || outData == NULL) {                 return -1;         }         p = strstr(czData, strSearch);         if (p == NULL) {                 return -1;         }         while (*p != '=' && *p != '\0') {                 p++;         }         if (*p == '\0') {                 return -1;         }         p++;         while (*p != ';' && *p != '\0') {                 *outData++ = *p++;         }         *outData = '\0';         return 0; }   赞

user: user-9
content: p++;   /////应去掉词句         while (*p != ';' && *p != '\0') {                 *outData++ = *p++;         }         *outData = '\0';         return 0; } O(∩_∩)O谢谢 2楼

user: user-9
content: ///引用2楼 的， int GetField(char *czData, char *strSearch, char *outData) {          char *p;   char *q=strSearch;    ////////////////         if (czData == NULL || strSearch == NULL || outData == NULL) {                 return -1;         }         p = strstr(czData, strSearch);         if (p == NULL) {                 return -1;         }         while (*p ==*q&& *q != '\0') {                  p++;                  q++;         }         if (*p == '\0') {                 return -1;         }         p++;         while (*p != ';' && *p != '\0') {                 *outData++ = *p++;         }         *outData = '\0';         return 0; } ///////////////////////////// 将2楼的修改下， 扩充性更好些

user: user-10
content: #include <stdio.h> #include <string.h> #define BUFSIZE 64 #define DEBUG enum tar_GF_ERR{ 	GF_Ok				= 0, 	GF_InputNullErr		=-1, 	GF_No_Match			=-2, };  int  GetField(char const *czData, char const *GetStr, char *outData);  int main() { 	int m = 1; 	int n; 	char* czData = "SN=12346;NB=abc;TB=688;"; 	char outData[BUFSIZE] = {0}; 	 	n = m+++m++;   // m++ + m++ 	printf("m = %d n = %d\n", m, n); 	 	if(!GetField(czData,"NB=",outData)) 		printf("outdata = %s\n", outData); }   int  GetField(char const *czData, char const *GetStr, char *outData) { 	char *start = NULL; 	char *end = NULL; 	int GetStrLen = 0; 	int LastLen = 0; 	int count = 0; 	 	if(!czData || !GetStr || !outData) 		return GF_InputNullErr; 		 	if(!(start = strstr(czData, GetStr)))   // if NULL 	{ 		strcpy(outData, ""); 		return GF_No_Match; 	} 	GetStrLen = strlen(GetStr);				// care 	if(GetStrLen > 0) 		start += GetStrLen; 		 	LastLen	= strlen(start); #ifdef DEBUG 	printf("DBG:LastLen = %d\n", LastLen); #endif 	end = start;		 	while(!(*end == ';') && LastLen--) 	{ #ifdef DEBUG 	printf("DBG:*end = %c\n", *end); #endif			 		end ++; 		count++; 	} #ifdef DEBUG 	printf("DBG:count = %d\n", count); #endif	 	if(LastLen == 0) 	{ 		strcpy(outData, ""); 		return GF_No_Match;		 	} 	else 	{ 		strncpy(outData, start, count); 	} 				 //	strcpy(outData, start);			// debug 	return GF_Ok; }

user: user-11
content: int GetField(char *czData, char *strSearch, char *outData) {          char *p;         if (czData == NULL || strSearch == NULL || outData == NULL) {                 return -1;         }         p = strstr(czData, strSearch);         if (p == NULL) {                 return -1;         }         while (*p != '=' && *p != '\0') {                 p++;         }         if (*p == '\0') {                 return -1;         }         p++;         while (*p != ';' && *p != '\0') {                 *outData++ = *p++;         }         *outData = '\0';         return 0; }

user: user-12
content: 没说清楚 “NB=”是会变的吗？ 函数原型写完整，类型什么的