用C语言分割字符串 C语言如何分割字符串
c\u8bed\u8a00\u4e2d \u5982\u4f55\u5206\u5272\u5b57\u7b26\u4e32c\u8bed\u8a00\u4e2d\u6ca1\u6709 string class, \u5047\u5b9a\u4f60\u6307 char \u5b57\u7b26\u4e32\u3002
\u7528 sscanf \u65b9\u6cd5\u53ef\u4ee5\u5206\u5272\u3002
char str2[]="aa 123,bb 456,cc 789,dd 000";
\u6309\u9017\u53f7\u5206\u5272\u4e3a4 \u90e8\u5206\uff1a
aa 123
bb 456
cc 789
dd 000
-----------------
char str[]="aa,bb,cc,dd";
\u6309\u9017\u53f7\u5206\u5272\u4e3a4 \u90e8\u5206\uff1a
aa
bb
cc
dd
-------------------------
#include
main()
{
char str[]="aa,bb,cc,dd";
char str2[]="aa 123,bb 456,cc 789,dd 000";
char s[4][10];
int i;
sscanf(str,"%[^,],%[^,],%[^,],%[^,]",s[0],s[1],s[2],s[3]); // \u7b2c1\u79cd\u65b9\u6cd5
for (i=0;i<4;i++) printf("%s\n",s[i]);
sscanf(str2,"%[^,],%[^,],%[^,],%[^,]",s[0],s[1],s[2],s[3]); // \u7b2c1\u79cd\u65b9\u6cd5
for (i=0;i<4;i++) printf("%s\n",s[i]);
//-----------
for (i=0;i<strlen(str);i++) if (str[i]==',') str[i]=' '; // \u7b2c2\u79cd\u65b9\u6cd5--\u66ff\u6362\u6210\u7a7a\u683c\u5206\u9694
sscanf(str,"%s %s %s %s",s[0],s[1],s[2],s[3]);
for (i=0;i<4;i++) printf("%s\n",s[i]);
return 0;
}
\u3000\u3000\u53ef\u4ee5\u5199\u4e00\u4e2a\u5206\u5272\u51fd\u6570\uff0c\u7528\u4e8e\u5206\u5272\u6307\u4ee4\uff0c\u6bd4\u5982cat a.c\u6700\u540e\u4f1a\u88ab\u5206\u5272\u6210cat\u548ca.c\u4e24\u4e2a\u5b57\u7b26\u4e32\u3001mv a.c b.c\u6700\u540e\u4f1a\u88ab\u5206\u5272\u6210mv\u548ca.c\u548cb.c\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002
\u3000\u3000\u53c2\u8003\u4ee3\u7801\u5982\u4e0b\uff1a
#include #include#define MAX_LEN 128void main(){ int i,length,ct=0,start = -1; char inputBuffer[MAX_LEN],*args[MAX_LEN]; strcpy(inputBuffer,"mv a.c b.c"); length=strlen(inputBuffer); for (i = 0; i <= length; i++) { switch (inputBuffer[i]){ case ' ': case '\t' : /* argument separators */ if(start != -1){ args[ct] = &inputBuffer[start]; /* set up pointer */ ct++; } inputBuffer[i] = '\0'; /* add a null char; make a C string */ start = -1; break; case '\0': /* should be the final char examined */ if (start != -1){ args[ct] = &inputBuffer[start]; ct++; } inputBuffer[i] = '\0'; args[ct] = NULL; /* no more arguments to this command */ break; default : /* some other character */ if (start == -1) start = i; } } printf("\u5206\u89e3\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u4e3a\uff1a\n"); for(i=0;i<ct;i++) printf("%s \n",args[i]);}
char delims_switchs[] = "|";
char *result_switchs = NULL;
result_switchs = strtok(szPortsMessage,delims_switchs);
while(result_switchs != NULL) {
printf( "result_switchs is \"%s\"\n",result_switchs);
char* tmp = result_switchs; ---------加一行,tmp=分割后的第一个字符串
result_switchs = strtok(NULL,delims_switchs); result_switchs 已经等于分割后的第二个字符串了
char swinfo[100];
strcpy(swinfo,tmp); ------------------换成tmp
char delims_switch[] = ";";
char *result_switch = NULL;
result_switch = strtok(swinfo,delims_switch);
while(result_switch != NULL) {
printf( "result_switch is \"%s\"\n",result_switch);
result_switch = strtok(NULL,delims_switch);
}
}
绛旓細涓嶉渶瑕佷笓闂鍒嗗壊锛C璇█閲岄潰鏈潵灏辨槸鐢ㄥ瓧绗︽暟缁勬潵淇濆瓨鐨勶紝濡傦細char a[20]="Hello world!";杩欎釜瀛楃涓锛宑har[0]灏辨槸H锛宑har[1]灏辨槸e銆傚鏋滆鍒嗗壊瀛愪覆锛屽彲浠ヤ娇鐢╯trtok鍑芥暟銆俢har *strtok(char *s, char *delim);鍒嗚В瀛楃涓蹭负涓缁勫瓧绗︿覆銆俿涓鸿鍒嗚В鐨勫瓧绗︿覆锛宒elim涓哄垎闅旂瀛楃涓层傞娆¤皟鐢ㄦ椂锛宻...
绛旓細include <stdio.h>#include <string.h>// 灏唖tr瀛楃浠pl鍒嗗壊,瀛樹簬dst涓紝骞惰繑鍥炲瓙瀛楃涓鏁伴噺int split(char dst[][80], char* str, const char* spl){ int n = 0; char *result = NULL; result = strtok(str, spl); while( result != NULL ) { strcpy(dst[n+...
绛旓細濡傛灉鏄粠杈撳叆璇伙紝閭d笉鐢ㄥ瓨瀛楃涓锛岀洿鎺ュ瓨鏁扮粍灏卞ソ浜 姣斿 scanf锛堚%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛夊鏋滃凡缁忔槸瀛楃涓蹭簡锛屾瘮濡俿tr锛岄偅涔堢敤sscanf 姣斿sscanf锛坰tr锛屸%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛
绛旓細鎶瀛楃涓瀛樻斁鍒版暟缁勯噷闈,涓涓竴涓瘮瀵癸紙寰幆锛夊悗杈撳嚭 鍒ゆ柇鏁板瓧锛歠or (i=0;i<n;i++){ if ((p[i]>='0') && (p[i]<='9'))printf();鍒ゆ柇瀛楁瘝锛歠or (i=0;i<n;i++){ if ((p[i]>='a') && (p[i]<='Z'))printf();...
绛旓細缁欏嚭浠g爜: #include <iostream>#include <cstdio>#include <cstring>#include <cctype>using namespace std;int main(){ int num,snum; num=0; snum=0; char s[100]; char zi[100]; char fu[100]; scanf("%s",s); for(int i=0;i<strlen(s);i++) ...
绛旓細鐩存帴鐢ㄧ畝鍗曠殑C++ include <iostream>#include <string>#include <vector>using namespace std;//鎶婂瓧绗︿覆s鎸夌収瀛楃涓瞔杩涜鍒囧垎寰楀埌vector_v vector<string> split(const string& s, const string& c){vector<string> v;int pos1=0,pos2;while((pos2=s.find(c,pos1))!=-1){v.push_back(s...
绛旓細int main(){ char buf[];//buf涓轰綘鐨勫甫绌烘牸鐨瀛楃涓 char arr1[]; //浠ヤ笅涓哄垎鍒敤浠ュ瓨鍌ㄧ殑瀛楃鏁扮粍 char arr2[];...int i = 0;int counts = 1; //寰幆璁℃暟 char*p =& buf[0]; //璇绘寚閽 while(*p!='\0'){ if(' '==*p){ p++;continue;} else { switch(counts)...
绛旓細= strtok(NULL, spl);} return n;} int _tmain(int argc, _TCHAR* argv[]){ char str[] = "123,456\n789,321";char dst[10][80];int cnt = split(dst, str, "\n");for (int i = 0; i < cnt; i++)puts(dst[i]);return 0;} 涓昏鏄瀛楃涓插垎鍓鍑芥暟strtok鐨勪娇鐢 ...
绛旓細瑕佽揪鍒拌繖鏍风殑鏁堟灉锛岀敤sscanf鐪嬫潵鏄悶涓嶅畾浜嗭紝鍙兘鑷繁鍐欏嚱鏁般備笅闈㈢殑鍑芥暟鐪嬫槸鍚︽弧瓒抽渶姹傦紝浣犲彲浠ュ湪鍏ㄥ眬鍙橀噺str[]閲屽姞浠绘剰澶氱殑璺緞锛屼絾鏈変竴鐐规敞鎰忕殑鏄紝瀛楃\鍦ㄤ覆閲屽繀椤昏浆涔変负\\锛岃矾寰勯暱搴︿笉瓒呰繃128浣嶅嵆鍙俰nclude <stdio.h> include <string.h> include <stdlib.h> char str[] = "clamscan c:\...
绛旓細char p[]="ab,cde,efgh,ghi32";char c=',';int i=0,j=0,m=0;while(p[i]){ if(p[i]==',')j++;i++;} j=j+1;//鎺ュ彈鍒板簳鏈夊灏戜釜鍗曡瘝鍦鍒嗗壊鍚庨氳繃閫楀彿鍘诲垽鏂 k=j;char**q=split(p,c,k);printf("\n");for(i=0;i<4;i++){ cout<<q[i];cout<<endl;} } ...