拆分带有任意个空格的任意字符串

  1. //字符串拆分函数,可以拆分任意长度的字符串
  2. //字符串可以在任意位置有任意多个空格
  3. //空格不用区分全角还是半角
  4. //此函数在BCB 6.0 Enterprise Suite 下测试通过
  5. //此代码版权归Micheal Ku所有,如用于商业用途须经过作者同意
  6. void TForm1::subFun(AnsiString str)
  7. {
  8. int i=0,j=0;
  9. strfull =str + " ";
  10. SQL ="";   //使用拆分出来的字符串的变量
  11. FullShape:    //全角空格处理标签
  12.           posfull = strfull.Pos(" ");
  13.           if(strfull.Pos(" ") > 0)
  14.           {
  15.                 strfulluse = strfull.SubString(1,posfull-1);
  16.                 strfullremain = strfull.SubString(posfull+2,strfull.Length()-posfull-1);
  17.                 strfull =strfullremain.Trim();
  18.                 strhalf = strfulluse.Trim();
  19.                 goto HalfShape;
  20.           }
  21.           else
  22.           if(strfull.Pos(" ") == 0)
  23.           {
  24.                 if(strfull != " " && strfull != " " && strfull != "")
  25.                 {
  26.                         //处理函数
  27.                         SQL += "测试字符串'%" + strfull;
  28.                         SQL += "', and ";
  29.                 }
  30.                 goto End;
  31.           }
  32. HalfShape:
  33.           poshalf = strhalf.Pos(" ");
  34.           if(strhalf.Pos(" ") == 0 )
  35.           {
  36.                 if(strhalf != " " && strhalf != " " && strhalf != "")
  37.                 {
  38.                         //处理函数
  39.                         SQL += "测试字符串'%" +  + strhalf;
  40.                         SQL += "', and ";
  41.                 }
  42.                 goto FullShape;
  43.           }
  44.           else
  45.           if(strhalf.Pos(" ") > 0)
  46.           {
  47.                 strhalfuse = strhalf.SubString(1,poshalf-1);
  48.                 strhalfremain =strhalf.SubString(poshalf+1,strhalf.Length()-poshalf);
  49.                 strhalf =strhalfremain.Trim();
  50.                 //处理函数
  51.                 if(strhalfuse != " " && strhalfuse != " "&& strhalfuse != "")
  52.                 {
  53.                         SQL += "测试字符串'%" +  + strhalfuse;
  54.                         SQL += "', and ";
  55.                 }
  56.                 goto HalfShape;
  57.           }
  58. End:
  59.           SQL += "测试成功";
  60.           ShowMessage(SQL);
  61. }
  62. //---------------------------------------------------------------------------

  因为笔者编程需要,要用到字符串拆分的功能,类似于搜索引擎查询时输入的一串字符串,不同的关键字以空格隔开,要求把这些关键字拆分开组成一段查询语句。

  

  笔者前期前递归调用算法写了个函数,执行效率极差,后来在网上查找了一下,多数是个拆分已知长度的字符串的例子,字符串的的空格个数非常有限,而且能查找的内容没有一个考虑到半角空格与全角空格以及多个空格同时存在的拆分问题。

 

        没有人能提供个较好的拆分算法及思路,笔者花了四个小时自己写了一个拆分的函数,经测试,完全正确。

 

  笔者使用的编译环境是BCB 6.0,测试完全可正常通过。

 

  使用时不要忘记在头文件中加上函数定义。

  

  头文件中标注下划线的一行为此函数的头文件定义内容。

  1. //---------------------------------------------------------------------------
  2. //字符串拆分子函数头文件
  3. //编译器BCB 6.0 Enterprise Suite
  4. #ifndef TestH
  5. #define TestH
  6. //---------------------------------------------------------------------------
  7. #include <Classes.hpp>
  8. #include <Controls.hpp>
  9. #include <StdCtrls.hpp>
  10. #include <Forms.hpp>
  11. //---------------------------------------------------------------------------
  12. class TForm1 : public TForm
  13. {
  14. __published:    // IDE-managed Components
  15.         TButton *Button1;
  16.         TEdit *Edit1;
  17.         TButton *Button2;
  18.         TEdit *Edit2;
  19.         void __fastcall Button1Click(TObject *Sender);
  20.         void __fastcall Button2Click(TObject *Sender);
  21. private:    // User declarations
  22. public:     // User declarations
  23.         __fastcall TForm1(TComponent* Owner);
  24.         void subFun(AnsiString str);
  25. };
  26. //---------------------------------------------------------------------------
  27. extern PACKAGE TForm1 *Form1;
  28. //---------------------------------------------------------------------------
  29. #endif

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值