CStringArray 分割字符串

 

 
//C++ 里分割字符串的三种做法。
void StringSplit(CString source, CStringArray& dest, char division)   
{   
   dest.RemoveAll();   
   
for(int i=0;i< source.GetLength();i++)   
   {   
      
if(source.GetAt(i)== division)   
      {   
         dest.Add(source.Left(i));         
//去掉右边   
         for(int j=0;j 〈 (dest.GetSize()-1);j++)   
         {   
            dest[dest.GetSize()
-1= dest[dest.GetSize()-1].Right(dest[dest.GetSize()-1].GetLength()-dest[j].GetLength()-1);  //去掉左边   
         }   
      }      
   }   
}  
//头文件里需要加上 #include <vector> 定义 vector 变量的方式为 vector<Cstring> ver;
    void StringSplit(CString inputString,char splitchar,vector<CString> &vec)
    {
        CString outStr;
        if (vec.size() > 0)
        {
            vec.clear();
        }
        int  len = inputString.GetLength();
        int  startIndex = 0;
        for (int i =0; i< len; i++)
        {
            if (inputString[i] == splitchar && i==0)
            {
                startIndex +=1;
            }
            if (inputString[i] == splitchar)
            {
                outStr = inputString.Mid(startIndex,i-startIndex);
                vec.push_back(outStr);
                startIndex = i+1;
            }
            if (i==len-1)
            {
               outStr = inputString.Mid(startIndex,i+1-startIndex);
               vec.push_back(outStr);
            }
        }
             
    }
//     inputstring :目标字符
//       splitChar:分割字符
//       saveStr:存储分割后的字符
    void StringSplit(CString inputString,char splitchar,CStringArray &saveStr)
    {
        CString outStr;
        int startIndex = 0;
         if (saveStr.GetSize() != 0)
         {
             saveStr.RemoveAll();
             for (int i=0;i<inputString.GetLength();i++)
             {
                 if (inputString[i] == splitchar && i==0)
                 {
                     startIndex +=1;
                 }
                 if (inputString[i]==splitchar)
                 {
                     outStr = inputString.Mid(startIndex,i-startIndex);
                     saveStr.Add(outStr);
                     startIndex = i+1;
                 }
                 if(i == inputString.GetLength() -1)
                 {
                     outStr = inputString.Mid(startIndex,i+1-startIndex);
                     saveStr.Add(outStr);
                 }
             }
         }
    }

*/

 

 

posted on 2010-10-29 08:41  遥望大海,云卷云舒  阅读(2357)  评论(0编辑  收藏  举报