zoukankan      html  css  js  c++  java
  • 几个字符串函数

    这几个函数都包含在StrUtils中,所以需要uses StrUtils;
    假设字符串是 Dstr := ’Delphi is the BEST’, 那么
    LeftStr(Dstr, 5) := ’Delph’
    MidStr(Dstr, 6, 7) := ’i is th’
    RightStr(Dstr, 6) := ’e BEST’

    ~~~~~~~~~~~~~~~~~~~~~~~~~
    function RightStr
    (Const Str: String; Size: Word): String;
    begin
    if Size > Length(Str) then Size := Length(Str) ;
    RightStr := Copy(Str, Length(Str)-Size+1, Size)
    end;
    function MidStr
    (Const Str: String; From, Size: Word): String;
    begin
    MidStr := Copy(Str, From, Size)
    end;
    function LeftStr
    (Const Str: String; Size: Word): String;
    begin
    LeftStr := Copy(Str, 1, Size)
    end;

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


    拆分字符串的函数 [2005-12-13]

    delphi中没有提供此类函数,从大富翁找了一个

    function split(src,dec : string):TStringList;
    var
    i : integer;
    str : string;
    begin
    result := TStringList.Create;
    repeat
    i := pos(dec,src);
    str := copy(src,1,i-1);
    if (str='') and (i>0) then
    begin
    delete(src,1,length(dec));
    continue;
    end;
    if i>0 then
    begin
    result.Add(str);
    delete(src,1,i+length(dec)-1);
    end;
    until i<=0;
    if src<>'' then
    result.Add(src);
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
    ss : TStringList;
    str,dec : string;
    begin
    str := '1111||2222||||3333|||4444||';
    dec := '||';
    ss := split(str,dec);
    memo1.Lines.AddStrings(ss);
    ss.Free;
    end;

  • 相关阅读:
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    CF #505 B Weakened Common Divisor(数论)题解
    HDU 6425 Rikka with Badminton(组合问题签到)题解
    ZOJ 2747 Paint the Wall(离散化+暴力)题解
  • 原文地址:https://www.cnblogs.com/jshchg/p/2180819.html
Copyright © 2011-2022 走看看