频道分类

delphi 根据字符串分隔符位置取值

作者:admin 来源: 日期:2020/3/28 22:05:35 人气: 标签:

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function StrSplit(SourStr: string; Delimiter: string): TArray<string>;
var
  i, len, count, count2: integer;
  sPart: string;
  sArr: TArray<string>;
begin

  len := Length(SourStr);
  sPart := '';
  count := 0;
  for i := 1 to len do
  begin
    if (SourStr[i] = Delimiter) or ((i = len) and (SourStr[i] <> Delimiter))
    then
    begin
      count := count + 1;
      SetLength(sArr, count);
      if (i = len) and (SourStr[i] <> Delimiter) then
        sPart := sPart + SourStr[i];
      sArr[count - 1] := sPart;
      sPart := '';
    end
    else
    begin
      sPart := sPart + SourStr[i];
    end;
  end;
  Result := sArr;
end;


// val1=123.22;val2=33.4;val4=55.5
function StrDelimiter (Sour: string; NumInString:integer): string;
var
  strValue, Item, sName, sValue: string;
  sArr: TArray<string>;
  i, j, n, lenArr, lenItem: integer;
  find: boolean;
begin
  Result := '';
  sArr := StrSplit(Sour, ';');

  lenArr := Length(sArr);
  if (lenArr > 0) and (NumInString-1 <= lenArr) then
    Result:= sArr[NumInString-1];
end;


function CountInStrDelimiter (Sour: string; Delimiter: string ): integer;
var
  sArr: TArray<string>;
  lenArr: integer;
begin
  Result := -1;
  sArr := StrSplit  (Sour, Delimiter);

  lenArr := Length(sArr);
  if (lenArr > 0)  then
    Result:= lenArr;
end;


procedure TForm1.FormCreate(Sender: TObject);
var
fgnum:integer;
begin

fgnum:=CountInStrDelimiter('val1=123.22;val2=33.4;val4=55.5',';');

Caption:='分割符数'+inttostr(fgnum)+' '+StrDelimiter('val1=123.22;val2=33.4;val4=55.5',2);

end;

end.

上一篇:delphi TLog 日志下一篇:没有资料