c++中字符串分割函数

it2026-04-03  9

鉴于在C++中没有可以用一个字符串来分割字符串的函数,所以特意写了一个方便大家:

其中str1是要分割的字符串,str2是用来分割的字符串。

CString* GetStr(CString str1, CString str2, int &num) {     int long1=str1.GetLength();  int long2=str2.GetLength();  int sign=0;  char*ch1=new char[long1];  char*ch2=new char[long2];  strcpy(ch1,str1);  strcpy(ch2,str2);   

    int i=0,j=0;

    while(1)  {     if(j==long1)break;        if(ch1[j]==ch2[i])     {      ++j;      ++i;      ++sign;      if(sign==long2)      {               i=0;      sign=0;      ++num;      }     }     else     {      j++;      sign=0;     }  }  num++;     //num++;

    i=0;  j=0;  sign=0;

 CString*str=new CString[num];  int begin=0,end=0;     int n=0;

 while(1)  {   if(j==long1)   {    //n++;          for(int k=begin;k<long1;k++)    {                str[n]+=ch1[k];    }    break;   }   if(ch1[j]==ch2[i])   {            ++j;      ++i;            ++sign;      if(sign==long2)      {               i=0;      sign=0;      end=j-long2;      for(int k=begin;k<end;k++)      {                   str[n]+=ch1[k];      }      begin=j;      n++;      }   }   else   {            j++;      sign=0;   }  }  

 return str; }

转载于:https://www.cnblogs.com/704486377/archive/2009/03/23/1419951.html

相关资源:c语言中字符串分割函数及实现方法
最新回复(0)