程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 用Delphi實現StringTokenizer功能

用Delphi實現StringTokenizer功能

編輯:Delphi

  我們知道Java中有StringTokenizer,對於字符串處理都非常的方便,但是Delphi中沒有提供該功能,我參考Java語言實現此功能的Delphi版本和C#版本。希望給大家提供一個方便。希望多多指教。
  
   unit SkyStringTokenizer;
  {字符串分析處理類
  作者:曾青松
  時間:2004/2/21(2003年除夕)
  }
  
  interface
  
  uses
  SysUtils, Classes;
  type
  IIterator = interface
  function hasNext(): boolean;
  function next(): string;
  end;
  type
  TSkyStringTokenizer = class(TComponent, IIterator)
  private
  tokens: TStringList;
  index: integer;
  data: string;
  delimiter: string;
  procedure init(dataLine: string; delim: string);
  function CharacterInTokens(ch: string; const sl: TStringList): boolean;
  function StringToCharArray(delim: string): TStringList;
  function SplitString(source, ch: string): Tstringlist;
  
  protected
  { Protected declarations }
  public
  { Public declarations }
  constructor create(dataLine: string); overload;
  constructor Create(dataLine: string; delim: string); overload;
  destructor destroy();override;
  function hasNext(): boolean;
  function next(): string;
  published
  { Published declarations }
  end;
  
  procedure Register;
  
  implementation
  
  procedure Register;
  begin
  RegisterComponents('SkyDevKit', [TSkyStringTokenizer]);
  end;
  
  { TStringTokenizer }
    function TSkyStringTokenizer.hasNext: boolean;
  begin
  result := (index < tokens.Count - 1);
  end;
  
  procedure TSkyStringTokenizer.init(dataLine, delim: string);
  begin
  tokens := TStringList.Create();
  delimiter := delim;
  data := dataLine;
  tokens := SplitString(data, delimiter);
  //tokens := data.Split(delimiter.ToCharArray());
  index := -1;
  end;
  
  function TSkyStringTokenizer.next: string;
  var
  s: string;
  begin
  inc(index);
  s := tokens[index];
  while ((length(s) <= 0) and (index < tokens.Count)) do
  begin
  inc(index);
  s := tokens[index];
  end;
  result := s;
  end;
  
  function TSkyStringTokenizer.StringToCharArray(delim: string): TStringList;
  var
  sl: TStringList;
  I: Integer;
  begin
  sl := TStringList.Create();
  for I := 1 to length(delim) do // Iterate
  begin
  sl.Append(copy(delim, i, 1));
  end; // for
  result := sl;
  end;
  
  constructor TSkyStringTokenizer.Create(dataLine, delim: string);
  begin
  inherited Create(nil);
  self.init(dataLine, delim);
  end;
  
  constructor TSkyStringTokenizer.Create(dataLine: string);
  begin
  inherited Create(nil);
  self.init(dataLine, ' ');
  end;
  
  function TSkyStringTokenizer.SplitString(source, ch: string): tstringlist;
  var
  temp: string;
  //i: integer;
  sl: TStringList;
  index: integer;
  begin
  if ch = '' then
  ch := ' ';
  
  sl := TStringList.Create();
  result := tstringlist.Create();
  sl := self.StringToCharArray(ch);

 temp := '';
  for Index := 1 to length(source) do // Iterate
  begin
 if not CharacterInTokens(source[index], sl) then
  begin
  temp := temp + source[Index];
  end
  else
  begin
  result.Add(temp);
  temp := '';
  end; //if
  end; // for
  if (temp <> '') then
  result.Add(temp);
  sl.Free;
  end;
  
  function TSkyStringTokenizer.CharacterInTokens(ch: string;
  const sl: TStringList): boolean;
  var
  index: integer;
  // I: Integer;
  begin
  result := false;
  for Index := 0 to sl.Count - 1 do // Iterate
  begin
  result := result or (ch = sl[

[1] [2] 下一頁

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved