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

Delphi實現字符串的二維表: TSta

編輯:Delphi

STA 單元 (用到 System.SysUtils.TStringHelper):

unit STA;
 
interfaceuses System.SysUtils, System.Classes;
 
type
  TSta = recordFSeparator: Char;
    FArr: TArray<TArray<string>>;constructor Create(const aStr: string; const 

aSeparator: Char = ';'); overload;classoperator Explicit(const aStr: string): 

TSta;classoperator Implicit(const aStr: string): TSta;function GetItem(i,j: Integer): 

string;procedure SetItem(i,j: Integer; Value: string);function GetRow(i: Integer): 

string;procedure SetRow(i: Integer; Value: string);procedure SetSeparator(const Value: 

Char);function GetRowCount: Integer;procedure SetRowCount(const Value: Integer);function 

ToString: string;procedure Clear;procedure LoadFromFile(const aFileName: string; aEncoding: 

TEncoding = nil);procedure SaveToFile(const aFileName: string; aEncoding: TEncoding = 

nil);property Separator: Char read FSeparator write SetSeparator;property RowCount: Integer read 

GetRowCount write SetRowCount;property Items[i,j: Integer]: stringread GetItem write SetItem; 

default;property Rows[i: Integer]: stringread GetRow write SetRow;
  end;
 
implementation{ TSta }procedure TSta.Clear;
begin
  SetLength(FArr, 0);
end;
 
constructor TSta.Create(const aStr: string; const aSeparator: Char);
var
  tArr: TArray<string>;
  i: Integer;
begin
  FSeparator := aSeparator;
  tArr := aStr.Split([sLineBreak], ExcludeEmpty);
  SetLength(FArr, Length(tArr));
  for i := 0to High(FArr) do
  beginFArr[i] := tArr[i].Split([FSeparator]);
  end;
end;
 
function TSta.GetItem(i,j: Integer): string;
begin
  Result := '';
  if (i < 0) or (j < 0) then Exit;
  if (i < Length(FArr)) and (j < Length(FArr[i])) thenResult := FArr[i, j].Trim;
end;
 
procedure TSta.SetItem(i,j: Integer; Value: string);
var
  k,n: Integer;
begin
  if Value.Trim = ''then Exit;
  if Length(FArr) = 0then FSeparator := ';';
  n := Length(FArr);
  if i >= n then
  beginSetLength(FArr, i+1);for k := n to i - 1do SetLength(FArr[k], 1);
  end;
  if j >= Length(FArr[i]) then SetLength(FArr[i], j+1);
  FArr[i,j] := Value.Trim;
end;
 
function TSta.GetRow(i: Integer): string;
begin
  Result := '';
  if i < Length(FArr) then
  beginif Length(FArr[i]) > 0then  Result := Result.Join(FSeparator, FArr[i]);
  end;
end;
 
function TSta.GetRowCount: Integer;
begin
  Result := Length(FArr);
end;
 
procedure TSta.SetRow(i: Integer; Value: string);
var
  k,n: Integer;
begin
  if Value.Trim = ''then Exit;
  if Length(FArr) = 0then FSeparator := ';';
  n := Length(FArr);
  if i >= n then
  beginSetLength(FArr, i+1);for k := n to i - 1do SetLength(FArr[k], 1);
  end;
  FArr[i] := Value.Split([FSeparator]);
end;
 
procedure TSta.SetRowCount(const Value: Integer);
begin
  SetLength(FArr, Value);
end;
 
procedure TSta.SetSeparator(const Value: Char);
begin
  FSeparator := Value;
  if Length(FArr) = 0then SetLength(FArr, 1); //直接使用索引賦值時, 會根據 Length(FArr) 是否為 0 

來設置默認分隔符end;
 
classoperator TSta.Explicit(const aStr: string): TSta;
begin
  Result := TSta.Create(aStr);
end;
 
classoperator TSta.Implicit(const aStr: string): TSta;
begin
  Result := TSta.Create(aStr);
end;
 
function TSta.ToString: string;
var
  i: Integer;
begin
  if Length(FArr) = 0then Exit('');
  Result := Rows[0];
  for i := 1to High(FArr) doResult := Result + sLineBreak + Rows[i];
end;
 
procedure TSta.LoadFromFile(const aFileName: string; aEncoding: TEncoding);
begin
  ifnot FileExists(aFileName) then Exit;
  if aEncoding = nilthen aEncoding := TEncoding.Default;
  with TStringList.Create dobeginLoadFromFile(aFileName, aEncoding);
    Self := Text;
    Free;
  end;
end;
 
procedure TSta.SaveToFile(const aFileName: string; aEncoding: TEncoding);
begin
  if aEncoding = nilthen aEncoding := TEncoding.Default;
  with TStringList.Create dobeginText := Self.ToString;
    SaveToFile(aFileName, aEncoding);
    Free;
  end;
end;
 
end.

測試:

uses STA;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  S: TSta;
  str: string;
begin
  S := 'AAA;BBB;CCC' + sLineBreak + '111;222;333'; //可以從字符串隱式或顯式地轉

換到 TSta
 
  str := S[0,0];    //AAA
  str := S[1,2];    //333
  str := S[9,9];    //越界讀取返回空
 
  str := S.Rows[0]; //AAA;BBB;CCC
  str := S.Rows[1]; //111;222;333
 
  str := S.ToString; //AAA;BBB;CCC //111;222:333
 
  S.Separator := '&'; //更換分隔符; 默認是分號; 也可在 Create 時指定
  str := S.Rows[1];   //111&222&333
  ShowMessage(str);
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  S: TSta;
  str: string;
begin
  S[0,0] := 'aaa';
  S[0,1] := 'bbb';
  S[0,2] := 'ccc';
  S[2,2] := 'zzz';
 
  str := S.ToString; //aaa;bbb;ccc // //;;zzz
 
  S.Rows[1] := '111;222;333';
  str := S.ToString; //aaa;bbb;ccc //111;222;333 //;;zzz//  ShowMessage(str);end;
 
procedure TForm1.Button3Click(Sender: TObject);
const
  nFileName = 'c:\temp\staTest.txt';
var
  S: TSta;
  str: string;
begin
  S[0,0] := 'aaa';
  S[0,1] := 'bbb';
  S[0,2] := 'ccc';
  S[1,0] := '111';
  S[1,1] := '222';
  S[1,2] := '333';
  S[1,3] := '444';
  S.SaveToFile(nFileName);   //保存到文件
  S.Clear;
  str := S[0,0]; //空//  ShowMessage(str);
 
  S.LoadFromFile(nFileName); //從文件讀取
  str := S[0,0];    //aaa
  str := S.Rows[0]; //aaa;bbb;ccc//  ShowMessage(str);end;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved