Welcome

首页 / 软件开发 / Delphi / Delphi实现字符串的二维表: TSta

Delphi实现字符串的二维表: TSta2013-12-12 cnblogs 万一STA 单元 (用到 System.SysUtils.TStringHelper):

unit STA; interfaceuses System.SysUtils, System.Classes; typeTSta = 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;beginSetLength(FArr, 0);end; constructor TSta.Create(const aStr: string; const aSeparator: Char);vartArr: TArray<string>;i: Integer;beginFSeparator := aSeparator;tArr := aStr.Split([sLineBreak], ExcludeEmpty);SetLength(FArr, Length(tArr));for i := 0to High(FArr) dobeginFArr[i] := tArr[i].Split([FSeparator]);end;end; function TSta.GetItem(i,j: Integer): string;beginResult := "";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);vark,n: Integer;beginif Value.Trim = ""then Exit;if Length(FArr) = 0then FSeparator := ";";n := Length(FArr);if i >= n thenbeginSetLength(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;beginResult := "";if i < Length(FArr) thenbeginif Length(FArr[i]) > 0thenResult := Result.Join(FSeparator, FArr[i]);end;end; function TSta.GetRowCount: Integer;beginResult := Length(FArr);end; procedure TSta.SetRow(i: Integer; Value: string);vark,n: Integer;beginif Value.Trim = ""then Exit;if Length(FArr) = 0then FSeparator := ";";n := Length(FArr);if i >= n thenbeginSetLength(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);beginSetLength(FArr, Value);end; procedure TSta.SetSeparator(const Value: Char);beginFSeparator := Value;if Length(FArr) = 0then SetLength(FArr, 1); //直接使用索引赋值时, 会根据 Length(FArr) 是否为 0 来设置默认分隔符end; classoperator TSta.Explicit(const aStr: string): TSta;beginResult := TSta.Create(aStr);end; classoperator TSta.Implicit(const aStr: string): TSta;beginResult := TSta.Create(aStr);end; function TSta.ToString: string;vari: Integer;beginif 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);beginifnot 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);beginif aEncoding = nilthen aEncoding := TEncoding.Default;with TStringList.Create dobeginText := Self.ToString;SaveToFile(aFileName, aEncoding);Free;end;end; end.