C语言中字符切割函数split的实现 (不使用库函数)实现字符串拆分函数strsplit(str1...
c\u8bed\u8a00\u81ea\u5df1\u7f16\u5199\u4e00\u4e2asplit\u5b57\u7b26\u4e32\u5206\u5272\u51fd\u6570\u8fd9\u4e2a\u662f\u7528c++\u5199\u7684\u4e0d\u8fc7\u548cc\u7684\u539f\u7406\u4e5f\u5dee\u4e0d\u591a\u6211\u8fd0\u884c\u8fc7\u4e86\uff0c\u6709\u4ec0\u4e48\u95ee\u9898\u518d\u95ee\u5427\uff0cc\u4e0d\u77e5\u9053\u80fd\u5426\u8fd0\u884c\uff0c\u4e0d\u8fc7\u73b0\u5728c/c++\u7a0b\u5e8f\u8bbe\u8ba1\u8bd5\u9a8c\u7cfb\u7edf\u901a\u5e38\u90fd\u6709\u5d4c\u5957c++\uff0c\u4f60\u6253\u5f00c\u754c\u9762\u65f6\u5355\u51fbvc6.0\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u8fd0\u884c\u5c31\u53ef\u4ee5\u4e86\u3002
#include
using namespace std;
char** split(char p[],char c,int k)
{
int i,m,j;
char **q;
q=(char**)new char*[k];
for(i=0;i<k;i++)
q[i]=(char*)new char*[20];
i=0;
j=0;
m=0;
while(p[i])
{
if(p[i]!=',')
{
q[j][m++]=p[i++];
}
else
{
q[j][m]='\0';
j++;
m=0;
i++;
}
}
q[k-1][m]='\0';
for(i=0;i<4;i++)
{
cout<<q[i];
cout<<endl;
}
return q;
}
void main()
{ int k;
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;//\u63a5\u53d7\u5230\u5e95\u6709\u591a\u5c11\u4e2a\u5355\u8bcd\u5728\u5206\u5272\u540e\u901a\u8fc7\u9017\u53f7\u53bb\u5224\u65ad
k=j;
char**q=split(p,c,k);
printf("\n");
for(i=0;i<4;i++)
{
cout<<q[i];
cout<<endl;
}
}
#include "stdafx.h"
#include
char a[10]="abcdefg";
int main(int argc, char* argv[])
{
void strsplit(char*,int,char*);
char *str1=a;
char *str2=new char;
*str2 =NULL;
int c=3;
printf("\u6e90\u5b57\u7b26\u4e32str1=%s\n",str1);
int a = strlen(str1);
strsplit(str1,c,str2);
printf("str1=%s\nstr2=%s\n",str1,str2);
str2 =NULL;
delete []str2;
return 0;
}
void strsplit(char* str1,int c,char* str2)
{
char *tmp=str1+c;
for (int i=c;i<strlen(str1);i++)
{
*str2 = *tmp;
*str2++;
*tmp++;
}
*str2++='\0';
str1+=c;
*str1 = '\0';
}
#include <stdio.h>
#include <string.h>
// 将str字符以spl分割,存于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++], result);
result = strtok(NULL, spl);
}
return n;
}
int main()
{
char str[] = "what is you name?";
char dst[10][80];
int cnt = split(dst, str, " ");
for (int i = 0; i < cnt; i++)
puts(dst[i]);
return 0;
}
c语言中 有切割字符串的函数啊!
strtok函数(const char *str, const char *ch)
第一个是字符串,第二个是以那个字符做切割。
例子:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[] = "123-sldkf-123ls-343434-dfjdlkfj-dflcmvn";
char *delim = "-";
char *p;
printf("%s ", strtok(s, delim));
while((p = strtok(NULL, delim))
printf("%s ", p);
printf("\n");
return 0;
}
char s[] = "dlkjf-sdlkfjsdf\lsdkljfl'sldkjf\sdkfjsdlf\lskdjfls";
如果想用-和\两个符号切割的话
char *delim = "-\";
这样就可以了
找了很久都没找到个靠谱的split函数。
我还是自己写一个分享在这里吧。英语不好请见谅:
/**
split the string by delimiter.
need to use "free" method to release the each of string in array.
@param targetString splited String
@param delimiter the delimiter to split the string
@param length the array`s length of result
@return array of splited string(need release manually)
*/
char** split(const char * const targetString, const char * const delimiter,int * const length){
*length=0;
char ** resultString=NULL;
char inputString[strlen(targetString)];
strcpy(inputString,targetString);
char inputDelimiter[strlen(delimiter)];
strcpy(inputDelimiter,delimiter);
char * splitedString = strtok(inputString, inputDelimiter);
while (splitedString){
(*length)++;
char * resultChar = NULL;
resultChar=malloc(strlen(splitedString));
strcpy(resultChar,splitedString);
resultString=realloc(resultString,(*length) * sizeof(char *));
resultString[(*length)-1] =resultChar;
splitedString=strtok(NULL, delimiter);
}
return resultString;
}
int main(int argc, const char * argv[]) {
char * ch1 ="abc
ABC
";
char * ch2 ="
";
char * ch3 ="";
int length1=0;
int length2=0;
char ** result1 = split(ch1, ch2, &length1);
for (int i=0;i<length1;i++){
// printf("%s
",result[i]);
char ** result2 = split(result1[i], ch3, &length2);
for (int j=0;j<length2;j++){
printf("%s--",result2[j]);
free(result2[j]); // release resource
}
printf("
");
free(result1[i]); // release resource
}
printf("Done
");
return 0;
}
绛旓細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+...
绛旓細杩欎釜鏄敤c++鍐欑殑涓嶈繃鍜c鐨鍘熺悊涔熷樊涓嶅鎴戣繍琛岃繃浜嗭紝鏈変粈涔堥棶棰樺啀闂惂锛宑涓嶇煡閬撹兘鍚﹁繍琛岋紝涓嶈繃鐜板湪c/c++绋嬪簭璁捐璇曢獙绯荤粺閫氬父閮芥湁宓屽c++锛屼綘鎵撳紑c鐣岄潰鏃跺崟鍑籿c6.0鍒涘缓涓涓」鐩繍琛屽氨鍙互浜嗐俰nclude <iostream> using namespace std;char** split(char p[],char c,int k){ int i,m,j;char ...
绛旓細瑕佷粠閿洏杈撳叆鍒楄〃锛屽彲浠ヤ娇鐢╥nput()鍑芥暟缁撳悎鍒楄〃鎺ㄥ寮忋傛偍鍙互鎸夌収浠ヤ笅绀轰緥浠g爜鐨鏍煎紡杩涜杈撳叆锛氫笂杩颁唬鐮佸皢浠庨敭鐩樿鍙栦竴涓瀛楃涓锛岃瀛楃涓插寘鍚敤绌烘牸鍒嗛殧鐨勫厓绱犮傜劧鍚庯紝split()鍑芥暟灏嗗瓧绗︿覆鎷嗗垎涓轰竴涓垪琛紝骞朵娇鐢╥nt()鍑芥暟灏嗘瘡涓厓绱犺浆鎹负鏁存暟绫诲瀷銆傛渶缁堬紝灏嗗緱鍒颁竴涓暣鏁板垪琛紝瀛樺偍鍦ㄥ悕涓簃y_list鐨勫彉閲...
绛旓細鍚箟锛氬厛鐢split('.')鏂规硶灏嗗瓧绗︿覆浠"."寮鍓插舰鎴愪竴涓瓧绗︿覆鏁扮粍锛岀劧鍚庡啀閫氳繃绱㈠紩[1]鍙栧嚭鎵寰楁暟缁勪腑鐨勭浜屼釜鍏冪礌鐨勫笺傛濊矾瑙f瀽锛歴plit()鏂规硶鏄皢鎸囧畾瀛楃涓叉寜鏌愭寚瀹氱殑鍒嗛殧绗﹁繘琛屾媶鍒嗭紝鎷嗗垎灏嗕細褰㈡垚涓涓瀛楃涓茬殑鏁扮粍骞惰繑鍥炪傛暟缁勪腑鐨勬瘡鍏冪礌閮藉悇鑷搴斾竴涓储寮曞硷紝灏卞ソ姣斿湪鏁版嵁搴撶殑琛ㄤ腑姣忚鏁版嵁绾綍...
绛旓細while( result != NULL ) { printf( "\"%s\"\n", result );result = strtok( NULL, str0 );} return 0;} char *strtok( char *str1, const char *str2 );鍑芥暟杩斿洖瀛楃涓str1涓揣闈犵潃鈥滃垎闅旂str2鈥鐨閮ㄥ垎鐨勬寚閽堬紝瀛楃涓瞫tr2浣滀负鏍囪鍒嗛殧绗︺傚鏋滄爣璁板垎闅旂娌℃湁鎵惧埌锛屽嚱鏁拌繑鍥濶ULL銆
绛旓細int main(int argc, char* argv[]){ void strsplit(char*,int,char*);char *str1=a;char *str2=new char;str2 =NULL;int c=3;printf("婧瀛楃涓str1=%s\n",str1);int a = strlen(str1);strsplit(str1,c,str2);printf("str1=%s\nstr2=%s\n",str1,str2);str2 =NULL;del...
绛旓細鐩存帴鐢ㄧ畝鍗鐨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...
绛旓細鐢↗Query鍐 function QueryStr(split) { split = split.split('.');var result = split[1];return result;} (function () { alert(QueryStr("www.baidu.com"));});浼氬脊鍑 鐢ˋsp.Net 鍐 string url = "www.baidu.com";string[] split = url.Split('.');string result = split[1]...
绛旓細1. 棣栧厛瑕佹槑鐧split鏂规硶鐨鍙傛暟鍚箟锛歱ublic String[] split(String regex)鏍规嵁缁欏畾鐨勬鍒欒〃杈惧紡鐨勫尮閰嶆潵鎷嗗垎姝瀛楃涓銆 2. 鐒跺悗灏辫鏄庣‘姝e垯琛ㄨ揪寮忕殑鍚箟浜嗭細'.'鐐 鍖归厤闄も淺n鈥濆拰"\r"涔嬪鐨勪换浣曞崟涓瓧绗︺'\'琛ㄧず杞箟瀛楃锛岃屽弻閲嶈浆涔夌浉褰撲簬鍘熸剰锛3. 鍥犳鈥淺\.鈥濆拰鈥.鈥濆悓鎰 4.鍏ㄥ彞鍗虫牴鎹'....
绛旓細for寰幆鐨勬剰鎬濇槸,濡傛灉褰撳墠鐨勫瓧绗涓嶆槸瀛楃涓茬粨鏉熺'\0'涔熶笉鏄┖鏍肩殑鏃跺欏氨鎶婅繖涓瓧绗︽墦鍗板埌灞忓箷涓,鐩村埌閬囧埌绌烘牸琛ㄧず鎵撳嵃瀹屼簡涓涓崟璇嶄簡,杩欐椂鍊欒烦鍑哄惊鐜墦涓涓洖杞︽崲琛屽嚭鏉,鍑嗗寮濮嬫墦涓嬩竴涓崟璇,浣嗘槸鍥犱负鍦╢or寰幆閲宨宸茬粡鑷姞浜,涓轰簡涓嶉敊杩囦笅涓涓崟璇嶇殑绗竴涓瓧绗,鎵浠ヨ鎶奿鍏堝噺涓,杩欐牱鐨勮瘽澶栭潰鐨勫ぇ寰幆鍐嶆墽琛...