分享

Delphi : 字符串截取函数LeftStr,MidStr,RightStr的用法

 容心居 2020-03-16

这几个函数都包含在StrUtils中,所以需要uses StrUtils;


举例:假设字符串是 Dstr := 'Delphi is the BEST', 那么
LeftStr(Dstr, 5) := 'Delph'
MidStr(Dstr, 6, 7) := 'i is th'
RightStr(Dstr, 6) := 'e BEST'

 

但我并不建议在Uses中引入StrUtils的做法,会增加程序体积,要用到其中哪个函数,直接写个Function就OK了。

  1. function RightStr
  2. (Const Str: String; Size: Word): String;
  3. begin
  4. if Size > Length(Str) then Size := Length(Str) ;
  5. RightStr := Copy(Str, Length(Str)-Size+1, Size)
  6. end;
  7. --------------------------------------
  8. function MidStr
  9. (Const Str: String; From, Size: Word): String;
  10. begin
  11. MidStr := Copy(Str, From, Size)
  12. end;
  13. --------------------------------------
  14. function LeftStr
  15. (Const Str: String; Size: Word): String;
  16. begin
  17. LeftStr := Copy(Str, 1, Size)
  18. end;

这几个函数经常结合Pos,Length,Copy函数使用。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多