全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3048
推到 Plurk!
推到 Facebook!

仿VB Split() 函式的自製 Delphi _split()函式。

 
Dalman
一般會員


發表:27
回覆:22
積分:24
註冊:2002-08-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-09-17 14:01:56 IP:211.21.xxx.xxx 未訂閱
【源起】 因小弟目前在客戶專案上碰到需將有分界字元的字串拆解成數個子字串,而Delphi 6 竟然缺席此一個重要應用的函式,因此自己 DIY 了一個。    【冀求】 若各位大大有更好的程式寫法,請不吝指教哦!    
//--------------------------------------------------------------------
//【函式作用】傳回分解後的字串群。
//【輸入引數】
//  vOriStr         欲分解的來源字串。
//  [vDelimiter]    選擇性引數,字串分隔字元。預設字元為',';
//【函式傳回】TStringList物件。
//【函式說明】
//‧此函式是否適用於「雙位元組」碼字串(即中英字混碼)?我還不能百分確定。
//‧使用此函式時,請加入 uses StrUtils。
//【作者版本】Dalman, 2002/09/17, 1.0.0。    function _split(const vOriStr: AnsiString; const vDelimiter: AnsiString=','): TStringList;
var
  i: Integer;
  r: TStringList;
  s, t: AnsiString;
begin
  try
    try
      r := TStringList.Create();
      s := vOriStr;
      if s <> '' then begin
        repeat
          i := Pos(vDelimiter, s);                                            //找出第一個Delimiter的索引位置。
          t := Trim(IfThen(i > 0, Copy(s, 1, i - 1), s));                     //若找到索引,則將子字串取出。
          r.Append(t);
          Delete(s, 1, i);
        until i = 0;
      end;
    except
      on e: Exception do; //ignore errors.
    end;
  finally
    Result := r;
  end;
end;
amide
一般會員


發表:4
回覆:13
積分:13
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-09-18 13:55:28 IP:211.22.xxx.xxx 未訂閱
Delphi有提供阿.... WrapText( Edit1.Text, #13#10 ,['.',' ',#9,'-'], 0 ); =======Help 如下=================== Splits a string into multiple lines as its length approaches a specified size. Unit SysUtils Category string handling routines function WrapText(const Line, BreakStr: string; nBreakChars: TSysCharSet; MaxCol: Integer):string; overload; function WrapText(const Line, MaxCol: Integer = 45):string; overload; Description WrapText scans a string for occurrences of any of the characters specified by nBreakChars and inserts a line-break, specified by BreakStr, at the last occurrence of a character in nBreakChars before MaxCol. Line is the text WrapText scans. MaxCol is the maximum line length. If the BreakStr and nBreakChars parameters are omitted, WrapText searches for space, hyphen, or tab characters on which to break the line and inserts a carriage return/line feed pair at the break points. WrapText does not insert a break into an embedded quoted string (both single quotes and double quotes are supported). For example, the following call wraps the text into two lines at the last space character: WrapText('The rain in Spain falls mainly on the plain.', #13#10, ['.',' ',#9,'-'], 42); \rThe result: The rain in Spain falls mainly on the plain.
amide
一般會員


發表:4
回覆:13
積分:13
註冊:2002-07-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-09-18 13:55:45 IP:211.22.xxx.xxx 未訂閱
Delphi有提供阿.... WrapText( Edit1.Text, #13#10 ,['.',' ',#9,'-'], 0 ); =======Help 如下=================== Splits a string into multiple lines as its length approaches a specified size. Unit SysUtils Category string handling routines function WrapText(const Line, BreakStr: string; nBreakChars: TSysCharSet; MaxCol: Integer):string; overload; function WrapText(const Line, MaxCol: Integer = 45):string; overload; Description WrapText scans a string for occurrences of any of the characters specified by nBreakChars and inserts a line-break, specified by BreakStr, at the last occurrence of a character in nBreakChars before MaxCol. Line is the text WrapText scans. MaxCol is the maximum line length. If the BreakStr and nBreakChars parameters are omitted, WrapText searches for space, hyphen, or tab characters on which to break the line and inserts a carriage return/line feed pair at the break points. WrapText does not insert a break into an embedded quoted string (both single quotes and double quotes are supported). For example, the following call wraps the text into two lines at the last space character: WrapText('The rain in Spain falls mainly on the plain.', #13#10, ['.',' ',#9,'-'], 42); \rThe result: The rain in Spain falls mainly on the plain.
jck1
一般會員


發表:53
回覆:67
積分:24
註冊:2002-05-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-09-20 10:06:52 IP:211.22.xxx.xxx 未訂閱
引言: 【源起】 因小弟目前在客戶專案上碰到需將有分界字元的字串拆解成數個子字串,而Delphi 6 竟然缺席此一個重要應用的函式,因此自己 DIY 了一個。 【冀求】 若各位大大有更好的程式寫法,請不吝指教哦!
//--------------------------------------------------------------------
//【函式作用】傳回分解後的字串群。
//【輸入引數】
//  vOriStr         欲分解的來源字串。
//  [vDelimiter]    選擇性引數,字串分隔字元。預設字元為',';
//【函式傳回】TStringList物件。
//【函式說明】
//‧此函式是否適用於「雙位元組」碼字串(即中英字混碼)?我還不能百分確定。
//‧使用此函式時,請加入 uses StrUtils。
//【作者版本】Dalman, 2002/09/17, 1.0.0。    function _split(const vOriStr: AnsiString; const vDelimiter: AnsiString=','): TStringList;
var
  i: Integer;
  r: TStringList;
  s, t: AnsiString;
begin
  try
    try
      r := TStringList.Create();
      s := vOriStr;
      if s <> '' then begin
        repeat
          i := Pos(vDelimiter, s);                                            //找出第一個Delimiter的索引位置。
          t := Trim(IfThen(i > 0, Copy(s, 1, i - 1), s));                     //若找到索引,則將子字串取出。
          r.Append(t);
          Delete(s, 1, i);
        until i = 0;
      end;
    except
      on e: Exception do; //ignore errors.
    end;
  finally
    Result := r;
  end;
end;
sorry,我對記憶體的觀念不是很清楚 如果我呼叫這個_split函數 他會自行建立一個tstringlist變數 那我該在哪裡free這個變數呢? 又 我在呼叫端寫了
  a:= _split('aaaaa, bbbbbb, ccccc, ddddd');
  for i:= 0 to a.count - 1 do begin
    memo1.lines.add(a.strings[i]);
  end;
  a.free;
我需要a.free嗎??還是不用, 那如果我下了a.free,free的是_split所建立的變數嗎? 我搞的不是很清楚 ><"
Dalman
一般會員


發表:27
回覆:22
積分:24
註冊:2002-08-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2002-09-20 17:20:08 IP:211.21.xxx.xxx 未訂閱
還是要執行 a.Free() 的,因為 _split() 每次執行時會動態產生一個「TStringList」物件instance,函式傳回的是這個「TStringList」instance指標,而a 變數內容只是儲存著這個「TStringList」instance指標。 若要清除 a 變數內容,只要單純地設定 a := nil 即可。 但是若要銷滅某個已建立物件且歸還物件在 runtime 時期所佔據的系統資源(例記憶體使用量,CPU耗用時間,硬碟空間等)的話,一定要使用物件所提供的Free()函式。
amide
一般會員


發表:4
回覆:13
積分:13
註冊:2002-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2002-09-21 09:58:45 IP:211.20.xxx.xxx 未訂閱
Delphi有提供字串分解的函式,可以不用自己寫的.很好用的... 分解後的資料可以放在TStringList. var sl : TStringList; begin sl:= TStringList.Create; // 這裡就是把分解後的字串放到TStringList sl.Text := WrapText( 來源字串, #13#10 ,[','], 0 ); sl.Free; end;
Dalman
一般會員


發表:27
回覆:22
積分:24
註冊:2002-08-21

發送簡訊給我
#7 引用回覆 回覆 發表時間:2002-09-24 10:46:04 IP:211.21.xxx.xxx 未訂閱
小弟看了 Delphi 6 線上文件及 WrapText() 函式原始碼後,有些小小心得:    ‧若單純分解字串成多列子字串,可以使用 WrapText() 函式。
‧WrapText() 函式的程式碼因「多適用性」功用關係,實作滿複雜的。
 且分解後的子字串會包含 Delimiter 字串,需多一道剔除字串作業,
 若用於 _split() 內勢必降低字串分解處理效率。
‧基於「真善美」的理想,個人又改良了 _split() 函式實作碼部份,功能一樣哦!    
//----------------------------------------------------------------------------
//【函式作用】傳回分解後的字串群。
//【輸入引數】
//  vOriStr         欲分解的來源字串。
//  [vDelimiter]    選擇性引數,字串分隔字元。預設=',';
//【函式傳回】TStrings物件。
//【作者版本】Dalman, 2002/09/24, 1.1.0。
//【函式說明】此函式可適用 MBCS 字集(即中英文字混合)。    function _split(const vOriStr: AnsiString; const vDelimiter: AnsiString=','): TStrings;
var
  i: Integer;
  s: AnsiString;
begin
  try
    result := TStringList.Create();
    s := Trim(vOriStr);
    repeat
      i := Pos(vDelimiter, s);                                              //找出第一個Delimiter的索引位置。
      if i = 0 then
        result.Append(s)
      else begin                                                            //若找到Delimiter時,則...
        result.Append(Copy(s, 1, i - 1));                                   //複製Delimiter字串之前的字串。
        Delete(s, 1, i);
      end;
    until i = 0;
  except
    on e: Exception do; //ignore errors.
  end;
end;
系統時間:2024-04-19 5:41:44
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!