[C語言]字串處理 – 以指定的字串分割字串(支援中文字元)

來源:互聯網
上載者:User

[C語言]字串處理 - 以指定的字串分割字串(支援中文字元)

 函數StringSplit(分割字串到一個字串數組中,其中該數組第0位為分割後字串的個數)
 函數StringSplit_Struct(以定義一個新結構的方式來實現該函數)

/*C代碼如下*/
#include <stdio.h>

/*實現方案1*/
/*分割字串到一個字串數組中,其中該數組第一位為分割後的個數*/
char** StringSplit(const char* string,const char* split)
{
      char** result;
      /*首先分配一個char*的記憶體,然後再動態分配剩下的記憶體*/
      result = (char * * )malloc(sizeof(char *)*1);
      memset(result,0,sizeof(char *)*1);
      /*定義一個遍曆用的指標和一個尋找位置用的指標*/
      char* p = string;
      char* pos = string;
      /*無論是否存在該分割串,絕對都會分割到一個字串*/
      int count = 1;
      while(*p != '\0')
      {
          char* temp;
          char* tt;
          /*尋找該字串*/
          pos = strstr(p,split);
          /*結果為0說明剩下的字串中沒有該字元了*/
          if(pos == 0)
          {
            result = (char * * )realloc(result,sizeof(char *)*(count+2));
            result[0] = count;
            result[count] = p;
            result[count+1] = NULL;
            return result;
          }
          /*分配臨時字串空間*/
          temp = (char * )malloc(sizeof(char)*(pos - p+1));
          memset(temp,0,sizeof(char)*(pos - p+1));
          /*設定頭指標,以便賦值時使用*/
          tt = temp;
          while(p<=pos)
          {
            *temp++ = *p++;
          }
          /*將字串結尾置零*/
          *--temp = '\0';
          result = (char * * )realloc(result,sizeof(char *)*(count+1));
          result[0] = count;
          result[count] = tt;
          count++;
          /*設定下一次遍曆時的指標(重要)。當split長度大於1時,不這樣設定會多賦值不必要的字串*/
          p +=strlen(split)-1;
      }
      return result;
}
/*實現方案2*/
/*為方便計數定義的結構,字串數組從0開始賦值*/
typedef struct{
      int number;         /*分割的字串個數*/
      char** string;         /*字串數組*/
}StringTab;
/*分割字串到一個字串數組中*/
StringTab StringSplit_Struct(const char* string,const char* split)
{
      StringTab result;
      /*首先分配一個char*的記憶體,然後再動態分配剩下的記憶體*/
      result.string = (char * * )malloc(sizeof(char *)*1);
      memset(result.string,0,sizeof(char *)*1);
      /*無論是否存在該分割串,絕對都會分割到一個字串*/
      result.number = 0;
      /*定義一個遍曆用的指標和一個尋找位置用的指標*/
      char* p = string;
      char* pos = string;
      while(*p != '\0')
      {
         char* temp;
         char* tt;
         /*尋找該字串*/
         pos = strstr(p,split);
         /*結果為0說明剩下的字串中沒有該字元了*/
         if(pos == 0)
         {
           result.string = (char * * )realloc(result.string,sizeof(char *)*(result.number+1));
           result.string[result.number] = p;
           return result;
         }
         /*分配臨時字串空間*/
         temp = (char * )malloc(sizeof(char)*(pos - p+1));
         memset(temp,0,sizeof(char)*(pos - p+1));
         /*設定頭指標,以便賦值時使用*/
         tt = temp;
         while(p<=pos)
         {
           *temp++ = *p++;
         }
         /*將字串結尾置零*/
         *--temp = '\0';
         result.string = (char * * )realloc(result.string,sizeof(char *)*(result.number+1));
         result.string[result.number] = tt;
         /*計數器加一*/
         result.number++;
         /*設定下一次遍曆時的指標(重要)。當split長度大於1時,不這樣設定會多賦值不必要的字串*/
         p +=strlen(split)-1;
      }
      return result;
}

int main()
{
      /*進行測試*/
      /*方案1測試*/
      char** array;
      array = StringSplit("a/aaa//哈aa","aaa");
      int i ;
      for(i=1;i<=(int)array[0];i++)
      {
          printf("Num:%d I:%d: Value: %s\n",array[0],i,array[i]);
      }
    
      array = StringSplit("a/aa哈a//哈aa","哈");
      for(i=1;i<=(int)array[0];i++)
      {
          printf("Num:%d I:%d: Value: %s\n",array[0],i,array[i]);
      }

      /*方案2測試*/
      StringTab array2;
      array2 = StringSplit_Struct("a/aaa//哈aa","aaa");
      for(i=0;i<=array2.number;i++)
      {
         printf("Num:%d I:%d: Value: %s\n",array2.number,i,array2.string[i]);
      }
      array2 = StringSplit_Struct("a/aa哈a//哈aa","哈");
      for(i=0;i<=array2.number;i++)
      {
         printf("Num:%d I:%d: Value: %s\n",array2.number,i,array2.string[i]);
      }

      return 0;
}

 

http://hi.baidu.com/nivrrex/item/8f0c8f175a9ce5721009b5a1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.